Monday, March 25, 2024

Dynamics 365 CE: Set lookup field value from a related record using JavaScript

 The following JavaScript code can be used to set a lookup field's value by retrieving the record from the child table and assign it to the parent table's related field on a form. 


Xrm.WebApi.retrieveRecord(entityName, '00000000-0000-0000-0000-000000000001', "?$select=field_name")
                .then(
                    function success(result1) {                                                                                                
                        var lookupValue             = new Array();
                        lookupValue[0]              = new Object();                        
                        lookupValue[0].id           = LookupFieldId;
                        lookupValue[0].name         = result1.field_name;
                        lookupValue[0].entityType   = "LookupEntityName";
                       
                        formContext.getAttribute("FormLookupAAttributeName").setValue(lookupValue);                            
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );

Regards,

Arun S Keerthi

No comments:

Post a Comment