Check Parent Entity Name From Child entity form D365 CE

Requirement
Add validation on child entity form based on parent entity.

Details
Sometimes we want to check name of the parent entity in the child entity quick create form for validation. We can use JavaScript for this validation.

For the demo let’s say when someone will try to create contact record from subgrid under Account we want to display a message, “Contact can’t be added here’ and want to disable all the fields. To do this we can use following script:

function ValidateParentEntity(executionContext) {
        debugger;
        var formContext = executionContext.getFormContext();
        var entityList = ["account"];
        var Context = executionContext.getContext();
        
        if (Context != null && Context.getQueryStringParameters() != null && Context.getQueryStringParameters().parentrecordtype != null) {

            var parententity = Context.getQueryStringParameters().parentrecordtype;
            if (entityList.includes(parententity)) {

                formContext.ui.setFormNotification('Contacts cannot be added here', 'ERROR', 'E101');

                var formControls = formContext.ui.controls;
                //disable all records
                formControls.forEach(control => {
                    if (control.getName() != "" && control.getName() != null) {
                        control.setDisabled(true);
                    }
                });
            }

        }
}

We can attach this method to contact quick create onload event and when we will try to create contact from the subgrid under account it will show like below
contactquickcreate


Summary

This is how we can check parent entity from child entity entity form.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply

Your email address will not be published. Required fields are marked *