Form component control in model driven app (how to access child form data from parent main form)

Form component is a great control used to work with related table (entity) records on a table form within a Power App. You can work with related table records by adding a form component control to another table’s main form.
In the below example, the form component control is used to load the contact form in the main account form, which also lets the user edit a contact record without leaving the account form.

Form component control added to an existing tab.

The usage and the setting up of the form customisation are all discussed in the below documentation from Microsoft. The control can be added as below.

Select the Form Component Control.

How to access the child form data from the main form JavaScript.

This little piece of code below will help to achieve, to access the child form field data from the main form script.

function getAttributeValue(executionContext, controlName, attributeName) {
    var formContext = executionContext.getFormContext();
    var quickViewControl = formContext.getControl(controlName);
    var myValue = null;
    if (quickViewControl != undefined) {
        if (quickViewControl.isLoaded()) {
            
            // Search by a specific column present in the control       
            myValue = quickViewControl.getControl().find(control => control.getName() == attributeName).getAttribute().getValue();

        }
        else {
            // Wait for some time and check again
            setTimeout(getAttributeValue, 10, executionContext);
        }
    }
    else {
        console.log("No data to display in the quick view control.");
        return;
    }
    return myValue;
} 

The control name is the name of the lookup field that you wrap the form control component. Beware that if you used the lookup two times in the form then you might need to identify the correct name of the control. Lets say for example if it is wrapped on top of customerid in case entity and the customer id lookup is added twice in the form, then the control name should be customerid1 instead of just customerid.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s