Lookups not working in fd.spBeforeSave

Hi,

I have some lookup fields on which validation is being carried out before the form is saved, basically it is checking if dropdown selection is blank or not and if not what is the value.

if (fd.field('BusUnit_x002f_Source').value == 11) works in fd.spRendered but not in fd.spBeforeSave saying no object and therefore validations do not work.

Another example.

alert(fd.field('BusUnit_x002f_Source').value) works in fd.spRendered... gives the proper value

alert(fd.field('BusUnit_x002f_Source').value) does not work in fd.spBeforeSave... gives undefined

What am I doing wrong? This only happens with lookup fields not with text fields

Thanks in advance

Dear @Daniel_Privitelli,
That's a little strange, but I would recommend using field validators instead of checking field values in code. Something like this should work better:

fd.spRendered(function(){
  fd.field('FieldName').validators.push({
    name: 'FieldNameValidator',
    error: 'Please, fill in the FieldName',
    validate: function(value) {
        if (!value) {
            return false;
        }

        return true;
    }
  });
});

Or it might be even better to just set field as required:

fd.spRendered(function(){
  fd.field('FieldName').required = true;
});

It might be necessary to wrap Lookup fields inside 'ready' method though.

Hi @Nikita_Kurguzov

Thanks for replying

What do you mean wrap fields in ready method? How is this done please?

And what if i need to validate on a specific value in a lookup on save for example? If value = 3 for example do something and if value is 5 do something else? How can I tackle this problem?

Thanks in advance

Dear @Daniel_Privitelli,
I didn't notice any issues running this code, can you send me any console errors you might be getting?

fd.spBeforeSave(function(){
  console.log(fd.field('Lookup').value);
  console.log(fd.field('Lookup').value.LookupId);
  if(fd.field('Lookup').value.LookupId == 7){
    alert('Hello, world!');
  }
});

As for the ready event, it's not necessary for all the actions, but it allows to make sure that the field is ready once the form loads:

fd.spRendered(function(){
  fd.field('Lookup').ready().then(function(field) {
    //do things here
  });
});

@Nikita_Kurguzov,

I have noticed that in the fd.spBeforeSave only fd.field('Lookup').value.LookupId works but fd.field('Lookup').value does not

alert(fd.field('Lookup').value); - Gives an " object [Object] " message
alert(fd.field('Lookup').value.LookupId); - Works correctly and gives the LookupId number

On the other hand, in fd.spRendered it works perfectly fine.

I had to recode everything referencing the field by LookupId for validations in spBeforeSave