Cannot read properties of null (reading Title)

Dear @Sternchen,
The better way to check lookup value is this:

if (fd.field('Division_x0020_lookup').value){
  isCement();
}

Lookup fields don't always have Title field, and the null value is not a string. If you want to check it when the form loads, you'll also need to use ready event. More on working with lookup field here - Lookup field — SharePoint forms

Ok thank you.
I tried it like this:

fd.field('Division_x0020_lookup').ready(function(field) {
alert(field.value.Title);
});

if (fd.field('Division_x0020_lookup').value && fd.field('Company_x0020_code_x0020_lookup').value){
    changeCurrency();
    isCement();

}

But the error appears.
image
Whats up with this error? How can I handle this?
Due to the error, the other error messages are not displayed.
Do I have to write my functions in the ready function so that they are not executed until the field has been written to? Or how exactly does this work? What is the deal with the alert.

Dear @Sternchen,
I am not sure what the error is - check browser's console for more information. Can you share the rest of the code on the form as well?

Here are the console logs. I can´t find anything special.


Do you really want the whole code? It ´s a very big form with a lot of code.

22_07.js (30.5 KB)
Here is the code. I put it in a js file.
22_07.xfds (91.3 KB)
and here is the form in case you need it too.

Thank you for helping me.

Dear @Sternchen,
If you remove all JavaScript code from the form, would you still get this error? Can you test?

The error only appears when I click the save button.
Without alle the code this is what happends:
image

Dear @Sternchen,
Try using this for lookup fields instead of checking Title:

if(fd.field('Lookup').value && fd.field('Lookup').value.LookupValue){
  //do something
}

But I need to check for a special Title.
Will it work like this?

function isCement(){
if (fd.field('Division_x0020_lookup').value.LookupValue== 'Cement' && fd.field('Selection_x0020_of_x0020_Busines').value == 'Customer SD'){
$(fd.field('Currency').$parent.$el).show();
I tried this but still the same error:


Or what do you mean?

Dear @Sternchen,
This will check for the selected value in the Lookup field as it appears in the field itself.

Try to isolate the error to a specific part of code - remove parts and see where the issue comes from, and then replace the part of the code with LookupValue instead of Title.

I can't say why, but it must be this code.

/fd.field('IBAN').addValidator({
name: 'IBAN validator',
error: 'Fill out the IBAN completely',
validate:function(value){
return value.indexOf('_') < 0
}
});
/

I commented it out and after that the error was gone. As soon as I comment it in again, it is there again.
Is there anything wrong with this code?

Before I switched to lookup fields, the code still worked. The IBAN field from the code also has nothing to do with a lookup field.

Dear @Sternchen,
Hmm, maybe there is no value when saving? You can add an extra check like this:

fd.field('IBAN').addValidator({
name: 'IBAN validator',
error: 'Fill out the IBAN completely',
validate:function(value){
  if(value){
   return value.indexOf('_') < 0
  }
  return true;
}
});

You can use return false; instead, if you want field to be filled out at all times.

Thank you. You are a lifesaver. :star_struck:
Hope this solve my problems for now.

1 Like

Dear @Nikita_Kurguzov ,

I am trying to hind a disable a lookup field if the first lookup field has not been selected yet, but no luck. Can you please advise as it is not disabling the second lookup at all. Please see example of my code below

fd.spRendered(function(){
  fd.control('Payroll_Area_2').ready().then(function(control) {
  function disableButton() {
      if (fd.field('Payroll_Area_2').value.LookupValue == '_')
          fd.field('Name').disabled = true;
       else {
          fd.field('Name').disabled = false;
      }
}

  // Calling disableComments when the Team Manager Comments value changes
  fd.field('Payroll_Area_2').$on('change',disableButton);

  // Calling disableComments on form loading
  disableButton();

});
})

Dear @Dina_Louw,
One thing is certain here - the Lookup value is never equal to "_", it's a very specific check. Instead, you can try something like this to check that there is no value:

fd.spRendered(function(){
  fd.control('Payroll_Area_2').ready().then(function(control) {
  function disableButton() {
      if (!fd.field('Payroll_Area_2').value)
          fd.field('Name').disabled = true;
       else {
          fd.field('Name').disabled = false;
      }
}

  // Calling disableComments when the Team Manager Comments value changes
  fd.field('Payroll_Area_2').$on('change',disableButton);

  // Calling disableComments on form loading
  disableButton();

});
})

Hi @Nikita_Kurguzov ,

Sorry just typed too quick. That did not do what I want.
What I want is that lookup 2 needs to be disabled until lookup 1 has a value. When lookup 1 has a value then I want lookup to be enabled. I found a way to disable Expat Name but when I choose an option on Payroll Area it is not enabling my Expat Name now....

Dear @Dina_Louw,
The code above should do just that. Check browser's console for errors, maybe something preventing the code from running.

This is what I get at the moment
image

Dear @Dina_Louw,
I didn't notice it before, but it's likely because you use fd.control() instead of fd.field() in the beginning.

It should be:

fd.spRendered(function(){
  fd.field('Payroll_Area_2').ready().then(function(control) {
  function disableButton() {
      if (!fd.field('Payroll_Area_2').value)
          fd.field('Name').disabled = true;
       else {
          fd.field('Name').disabled = false;
      }
}

  // Calling disableComments when the Team Manager Comments value changes
  fd.field('Payroll_Area_2').$on('change',disableButton);

  // Calling disableComments on form loading
  disableButton();

});
})

Try this and check the console again if it doesn't work!

Hi @Nikita_Kurguzov,

Thank you sooo much, I too overlooked that and did not realize. Thank you kindly, it works perfectly.

1 Like