JavaScript For Querying An Entity And Updating Attribute Values In Form

Introduction

This article demonstrates how to query an entity using FetchXML, JavaScript (XRMServiceToolKit), and how to update the form fields on form load or on change event.

Steps to implement JavaScript function in Dynamics CRM Form

JavaScript filename - TotalValueUpdate.js 

  1. function TotalValues(FieldName, TotalFieldName) {  
  2.     var countval = Xrm.Page.getAttribute("new_count").getValue(); // To get the field value of count – used in FetchXML query  
  3.     var quarter = Xrm.Page.getAttribute("new_quarter").getValue(); // To get the field value of quarter – used in FetchXML query  
  4.     quarter = quarter - 1;  
  5.     var FieldValue = Xrm.Page.getAttribute(FieldName).getValue();  
  6.     var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + " <entity name='new_report'>" + " <attribute name='new_reportid' />" + " <attribute name='new_name' />" + " <attribute name='" + TotalFieldName + "' />" + " <attribute name='createdon' />" + " <order attribute='new_name' descending='false' />" + " <filter type='and'>" + " <condition attribute='new_count' operator='eq' value='" + countval + "' />" + " <condition attribute='new_quarter' operator='eq' value='" + quarter + "' />" + " </filter>" + " </entity>" + "</fetch>"  
  7.     var Records = XrmServiceToolkit.Soap.Fetch(fetchXML);  
  8.     if (Records.length > 0) {  
  9.         var prevTotalValue = Records[0].attributes[TotalFieldName].value;  
  10.         var curTotalValue = prevTotalValue + FieldValue  
  11.         Xrm.Page.getAttribute(TotalFieldName).setValue(curTotalValue);  
  12.         return true;  
  13.     } else {  
  14.         Xrm.Page.getAttribute(TotalFieldName).setValue(FieldValue);  
  15.         return false;  
  16.     }  
  17. }   

To add the above JavaScript file to Web Resource in Microsoft Dynamics CRM, please follow the below steps.

Go to the top ribbon, select the Settings icon and from there, click on the "Solutions" to create a new custom solution.


To create new solution, provide the values to the yellow highlighted fields shown below.

Then, select Web Resources from the left navigation and upload the JavaScript file using New icon (yellow highlighted).



Click on the Form Editor of the Report form to configure how and when the JavaScript function should be called on change event of a field.

Now, select the field and Change Properties from the top ribbon.

Select the Events tab and include all the highlighted JavaScript files by clicking "Add" icon. The most important thing in Event Handlers section is to select the Event OnChange and map the TotalValues() from the TotalValueUpdate.js.

Now, go to the Events Handler section, click on the Add button to mention the JavaScript library name from the dropdown and to specify the function name. Then, click OK once you are done.



Final output

Now, go to the entity form and check the value of Total values after entering this quarter value. It will be automatically adding the quarter value to the previous total and updating it to the current total value field.

I hope this article will be helpful for you to understand how to query Dynamics CRM Entities and update attributes in Form using JavaScript.


Similar Articles