Tag: JavaScript

Dynamics 365 – Open entity form using Javascript (with Parameters)

I had a function in a CRM upgrade that was nice and small.

locAddRelatedTo(crmFormSubmit.crmFormSubmitObjectType.value);

It would open up a copy of the entity I was on with some lookups populated, great right? Well it was. Back in CRM 4 days but not much use now.

Instead you now need to use Xrm.Utility.openEntityForm as below:-

    var parameters = {};
    var childEntityName = Xrm.Page.data.entity.getEntityName();

    //Provide parent record guid
    parameters["_CreateFromId"] = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");

    //Provide parent record entity type code
    parameters["_CreateFromType"] = Xrm.Page.context.getQueryStringParameters().etc;


    if (Xrm.Page.getAttribute("sampleLookup").getValue() != null) {
        parameters["sampleLookupFieldName"] = Xrm.Page.getAttribute("sampleLookupFieldName").getValue()[0].id.replace("{", "").replace("}", "");

// Note the name on the end
        parameters["sampleLookupFieldNamename"] = Xrm.Page.getAttribute("sampleLookupFieldName").getValue()[0].name;

// Not the type on the end
        parameters["sampleLookupFieldNametype"] = Xrm.Page.getAttribute("sampleLookupFieldName").getValue()[0].entityType;
    }
    
    if (Xrm.Page.getAttribute("sampleFieldName").getValue() != null) {
        parameters["sampleFieldName"] = Xrm.Page.getAttribute("sampleFieldName").getValue();
    }

Xrm.Utility.openEntityForm(childEntityName, null, parameters);

This opens a new form BUT throws an error. You then need to go into your Customizations, find the form, go to Parameters and make sure that all parameters are listed like so.

Parameters

Then the form should display with fields pre-populated.