Quantcast
Channel: Microsoft Dynamics CRM
Viewing all articles
Browse latest Browse all 123975

Forum Post: RE: How to auto-fill a field from look-up data based on the selection from option set?

$
0
0

You might have to better define what you are looking to do exactly. But this example fires on the OnChange event of the Account record's Category (option set) field and sets the Territory (lookup) field based on a query to the REST endpoint. Hopefully it will give you a head start on what you need to accomplish.

function Category_OnChange() {     //replace with your option set field     var category = Xrm.Page.getAttribute("accountcategorycode").getValue();      if (category == null) {         //replace with your lookup field         Xrm.Page.getAttribute("territoryid").setValue(null);     }      var name;     //adjust to match option set values     switch (category) {     case 1:         name = "Asia Pacific";         break;     case 2:         name = "EMEA";         break;     }      var serverUrl = Xrm.Page.context.getClientUrl();     //adjust based on the entity and fields you are searching      var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/TerritorySet?$select=TerritoryId,Name&$filter=Name eq '" + name + "'";      var retrieveReq = new XMLHttpRequest();     retrieveReq.open("GET", oDataSelect, false);     retrieveReq.setRequestHeader("Accept", "application/json");     retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");     retrieveReq.onreadystatechange = function () {         GetTerritoryData(this);     };     retrieveReq.send(); }  function GetTerritoryData(retrieveReq) {     if (retrieveReq.readyState == 4) {         if (retrieveReq.status == 200) {             var retrieved = JSON.parse(retrieveReq.responseText).d;             if (retrieved.results.length == 1) {                 var value = new Array();                 value[0] = new Object();                 value[0].id = retrieved.results[0].TerritoryId;                 value[0].name = retrieved.results[0].Name;                 value[0].entityType = "territory";                 //replace with your lookup field                 Xrm.Page.getAttribute("territoryid").setValue(value);             } else {                 //replace with your lookup field                 Xrm.Page.getAttribute("territoryid").setValue(null);             }         }     } } 

Viewing all articles
Browse latest Browse all 123975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>