Sunday, June 14, 2020

How to set field value on change of lookup value using JavaScript in D365

Remove & set fields value on change of a lookup value

I have written a function below to remove and update different types of field values on the change of the lookup field value.
Follow the below code to remove and update the field/attribute value on OnChange of a lookup field value.

Call the function on the OnChange event of the field for which you want to trigger.

In the below code i'm removing the attribute value on OnChange of the lookup value. These attributes are present on the connection entity. 
To set the data I'm using a web API call to fetch the value from the systemuser and updating the same to the connection entity record on the OnChange event.

Get the Lookup field Id based on the field id do call web API like below

Note: Replace the new_  prefix with your prefix that you are using in your solution for your attributes/entities. 

Here is the code:


function OnchangeUpdateContactDetails (executionContext) {
            var formContext = executionContext.getFormContext();
            formContext.getAttribute("new_email").setValue(null);
            formContext.getAttribute("new_officephone").setValue(null);
            formContext.getAttribute("new_mobilephone").setValue(null);
            formContext.getAttribute("new_countryid").setValue(null);

            var userId = formContext.getAttribute("record2id").getValue();
            if (userId != null && userId[0].id != null) {
                userId = userId[0].id;
                Xrm.WebApi.online.retrieveMultipleRecords("systemuser", "?$select=internalemailaddress,mobilephone,address1_telephone1,_new_countryid_value&$filter=systemuserid eq '" + userId + "'").then(
                    function success(results) {
                        for (var i = 0; i < results.entities.length; i++) {
                            if (results.entities[i]["internalemailaddress"]) {
                                var emailId = results.entities[i]["internalemailaddress"];
                                formContext.getAttribute("new_email").setValue(emailId);
                            }
                            if (results.entities[i]["address1_telephone1"]) {
                                var officePhone = results.entities[i]["address1_telephone1"];
                                formContext.getAttribute("new_officephone").setValue(officePhone);
                            }
                            if (results.entities[i]["mobilephone"]) {
                                var mobilePhone = results.entities[i]["mobilephone"];
                                formContext.getAttribute("new_mobilephone").setValue(mobilePhone);
                            }
//Set lookup field value
                            if (results.entities[i]["_new_countryid_value"]) {
                                var countryLookup = new Array();
                                countryLookup[0] = new Object();
                                countryLookup[0].id = results.entities[i]["_new_countryid_value"];
                                countryLookup[0].name = results.entities[i]["_new_countryid_value@OData.Community.Display.V1.FormattedValue"];
                                countryLookup[0].entityType = results.entities[i]["_new_countryid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                                formContext.getAttribute("new_countryid").setValue(countryLookup);
                            }
                        }
                    },
                    function (error) {
                        Xrm.Navigation.openAlertDialog(error.message);
                    }
                );
            }

        }

------------------------------------------------------------------------------------------------------------