Populate List or Library field based on parent condition

Hello,

I have a parent form and child List or Library control called 'SPDataTable1' that needs to stay in inline mode.

When parent field value = 'Surgical masks' I would like SPDataTable1 field 'item' to = 'Surgical masks' and restrict the 'Quantity' field to '6'.

Is this possible when using a List or Library control inline?

Thank you!

Hello @ParAvion,

Yes, you can populate fields in List or Library List or Library control in the inline editing mode. Please find the code example in the Manipulating fields within List or Library in Inline editing mode article.

Please see the code example below that restricts the Quantity column to be equal 6. Please make sure that you are using the internal name of the column in the code.

fd.spRendered(function() {
  fd.control('SPDataTable1').$on('edit', function(editData) {

      editData.field('Currency').validators.push({
        name: 'MyCustomValidator',
        error: '',
        validate: function(value) {
            if (value != 6) {
                this.error = 'Value must by greater than 0';
                return false;
            }
            return true;
        }
      });
  });
});

@mnikitina I can't seem to get this working.

When user selects 'Surgical Mask Replacement' from the parent field, i want 'Surgical masks' to auto-populate in the List or Library control when New Item is created:

This is the code I'm using now:

 fd.spRendered(function() {

    function setSurgical() {
        if (fd.field('BusiinessJustification').value == 'Surgical Mask Replacement') {
            // Setting the ProjectHealth to Major Concerns
            editData.field('Title').value = 'Surgical masks';

        }
    }

    // Calling setPercentComplete when the user changes the status
    fd.field('BusiinessJustification').$on('change',setSurgical);

    // Calling setProjectStatus on form loading
    setSurgical();

});

Hello @ParAvion,

Please see Populating fields of a new row in List or Library control point of the article with the code example.

For your case, you need to add the condition, please see the example below:

fd.spRendered(function() {
    //prepopulating fields of a new record
    //with the values from the parent form
    fd.control('SPDataTable1').$on('edit', function(editData) {
        //check that this is a new record
        if (editData.formType === 'New') {
            if(fd.field('BusiinessJustification').value == 'Surgical Mask Replacement') {
                //prepopulating title
                editData.field('Title').value = 'Surgical masks';
            }
        }
    });
});

@mnikitina thanks for your response. I forgot to mention that the 'Title' column is a lookup. So I modified your code to reference "value.LookupValue" but it's still not working. Any suggestions?

fd.spRendered(function() {
//prepopulating fields of a new record
//with the values from the parent form
fd.control('SPDataTable1').$on('edit', function(editData) {
//check that this is a new record
if (editData.formType === 'New') {
if(fd.field('BusiinessJustification').value == 'Surgical Mask Replenishment') {
//prepopulating title
editData.field('Title').value.LookupValue = 'Surgical masks';
}
}
});
});

Hello @ParAvion,

You can set the lookup value with the ID only, please see the code below.

fd.spRendered(function() {
    //prepopulating fields of a new record
    //with the values from the parent form
    fd.control('SPDataTable1').$on('edit', function(editData) {
        //check that this is a new record
        if (editData.formType === 'New') {
            if(fd.field('BusiinessJustification').value == 'Surgical Mask Replacement') {
                //prepopulating title
                editData.field('Title').ready().then(function() {
                      //replace 0 with the item ID
                      editData.field('Title').value = 0;
                });
            }
        }
    });
});

@mnikitina thanks it worked! Just one more question:

if (fd.field('BusiinessJustification').value == 'Surgical Mask Replacement'), restrict SPDataTable1 field 'Quantity' value to a maximum of 6. ('Quantity' is not a lookup field).

Thank you!

Hello @ParAvion,

You need to add an additional condition to the validation function, like this:

fd.spRendered(function() {
  fd.control('SPDataTable1').$on('edit', function(editData) {

      editData.field('Currency').validators.push({
        name: 'MyCustomValidator',
        error: '',
        validate: function(value) {
            if (fd.field('BusiinessJustification').value == 'Surgical Mask Replacement') {
                if (value >= 6) {
                    this.error = 'Value must by less than equal to 6';
                    return false;
                }
            }
            return true;
        }
      });
  });
});

@mnikitina is it possible to ad a validation function on the Child 'SPDataTable1' green check mark? I want the green check mark to be required to check off prior to saving the Parent form.

Thanks agian!

Hello @ParAvion,

Yes, you can add a custom validator that checks if a control has unsaved lines. Please find the code below.

fd.validators.push({
    name: 'MyCustomValidator',
    error: "Please save rows",
    validate: function(value) {
        return fd.control('SPDataTable0').widget.element.find('.k-i-check').length==0;
    }
});