If you are utilizing Main Form Dialogs or a Quick Create form to create a record from a lookup field in a parent form, you likely want to prefill one or more fields with the content from the parent form. For instance, if your parent form contains both account and contact information, you might want to enable users to directly create both the account and contact using Main Form Dialogs. Unfortunately, when creating the contact (for example), there is no straightforward way to prefill the account field in the contact form.

There are two options to solve this problem:

  1. Mapping of fields
  2. Javascript

Mapping of fields

To solve the problem I tried mapping (for more information about mapping: https://learn.microsoft.com/en-us/power-apps/maker/data-platform/map-entity-fields). Unfortunately mapping the data from the parent table to the contact table is not possible . Since it is an N:1 relationship, mapping is only feasible the other way around…

However, it is possible to create a new 1:N relationship with the desired mapping. First create a lookup field in the child table (contact table in my example), like “originating <tablename>”. From there add the mapping to this relationship:

After this change, the account name will be prefilled when I create a new contact record by using the Main Form Dialog:

Downside of this mapping option is that you have to save the parent record first; otherwise, the fields are not mapped. If that is a problem, you might use the Javascript option.

Javascript

To solve this issue by using javascript, you have to save the account id and account name in the parent form to a local storage variable. This function has to be executed onLoad of the form and onChange of the account field:

function SetLocalStorage(executionContext)
{
var formContext = executionContext.getFormContext();
var account = formContext.getControl("cw_supplier").getAttribute().getValue();

if(account != null) { 
   var accountid = account[0].id.toString().replace('{','').replace('}',''); 
   var accountname = account[0].name.toString(); 

    localStorage.setItem("AccountId",accountid );
    localStorage.setItem("AccountName",accountname );

    }
}

Now when the contact main of quick create form is opened, the local storage items may be used to fill in the account field. To do so, add the following function to the onLoad property of the form:

function RetrieveStorageValue(executionContext)
{
var formContext = executionContext.getFormContext();
var accountid = localStorage.getItem("AccountId");
var accountname = localStorage.getItem("AccountName");

if (accountid != null) {

        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = accountid;
        lookupValue[0].entityType = "account";
        lookupValue[0].name = accountname;
        formContext.getAttribute("parentcustomerid").setValue(lookupValue);
      
        localStorage.removeItem("AccountId");
        localStorage.removeItem("AccountName");
        }
  
}

I hope this helps. If you have any questions or remarks, please let me know (elowy@powerpro.nl)!

Laat het ons weten!

Neem gerust contact op als je wat meer van onze dienstverlening wilt weten.