Implementing Field Mapping Using Xrm.WebApi

In our last article, we discussed Xrm.WebApi Create, Update, and Delete method. In this article, we are going to provide sample code for using Xrm.WebApi Retrieve method.

Many times, we required to set up field mapping between the parent and child entities, for example - let’s say we need to bring the following information based on the account selected under Contact entity. We can easily set these fields' mapping by editing the Account and Contact relationship. But, that relationship mapping will only work when the contact record is created from the Account entity form.

 
However, if we are creating the contact record directly, the relationship mapping won’t work here. So, in this article, we are providing a sample code to implement the above mapping using Retrieve method.
  1. //retrieve data based on primary entity id  
  2. function retrieveAccountDetails() {  
  3.   //read lookup value  
  4.   if (Xrm.Page.getAttribute("parentcustomerid").getValue() != null && Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id != null) {  
  5.     var accountid = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id;  
  6.    
  7.     //pass entity, fields, we can use expand to get related entity fields  
  8.     Xrm.WebApi.retrieveRecord("account", accountid, "?$select=telephone1,new_verificationrequired,new_activationdate,address1_shippingmethodcode&$expand=parentaccountid($select=accountid,name)").then(  
  9.       function success(result) {  
  10.         if (result != null) {  
  11.           //set text field  
  12.           if (result.telephone1 != null)  
  13.             Xrm.Page.getAttribute("telephone1").setValue(result.telephone1);  
  14.           //set lookup field  
  15.           if (result.parentaccountid != null) {  
  16.             var object = new Array();  
  17.             object[0] = new Object();  
  18.             object[0].id = result.parentaccountid.accountid;  
  19.             object[0].name = result.parentaccountid.name;  
  20.             object[0].entityType = "account";  
  21.             Xrm.Page.getAttribute(new_parentaccount).setValue(object);  
  22.           }  
  23.           //set two optionset  
  24.           if (result.new_verificationrequired != null)  
  25.             Xrm.Page.getAttribute("new_verificationrequired").setValue(result.new_verificationrequired);  
  26.           //set date field  
  27.           if (result.new_activationdate != null)  
  28.             Xrm.Page.getAttribute("new_activationdate").setValue(new Date(result["[email protected]"]));  
  29.           //set optionset field  
  30.           if (result.address1_shippingmethodcode != null)  
  31.             Xrm.Page.getAttribute("address1_shippingmethodcode").setValue(result.address1_shippingmethodcode);  
  32.         }  
  33.       },  
  34.       function(error) {  
  35.         alert(error.message);  
  36.    
  37.       }  
  38.     );  
  39.   }  
  40. }  

The above code is self-explanatory. It can be modified based on the required fields that we want to fetch from the parent entity and can call the above function on onChange of the lookup field for parent entity. For example, in the above scenario, it can be used on onChange of the "parentcustomerid" lookup field.

Stay tuned for more Dynamics 365 Sample code !!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.