Advertisement
StreckerCM

CRM Get Financial Status

Jan 2nd, 2020
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkAssetFinancialStanding(executionContext) {
  2.     var formContext = executionContext.getFormContext();
  3.  
  4.     var createBookingField = formContext.getControl("hha_bookingcreate");
  5.  
  6.     var financialstanding;
  7.  
  8.     var customerAsset = formContext.getAttribute("msdyn_customerasset").getValue()
  9.  
  10.     var assetId = customerAsset[0].id;
  11.  
  12.     console.log("Asset Guid : " + assetId);
  13.  
  14.     SDK.REST.retrieveRecord(
  15.         assetId,
  16.         "msdyn_customerasset",
  17.         null, null,
  18.         financialstanding = getAssetDetails,
  19.         errorHandler
  20.     );
  21.    
  22.     if (financialstanding !== "good standing") {
  23.  
  24.         var actionCollection = {
  25.             message: "Please set value of the Bonus Potential field",
  26.             actions: null
  27.         };
  28.  
  29.         createBookingField.addNotification({
  30.             messages: ["Set Bonus Potential"],
  31.             notificationLevel: "RECOMMENDATION",
  32.             uniqueId: "001",
  33.             actions: [actionCollection]
  34.         });
  35.     } else {
  36.         createBookingField.clearNotification("001");
  37.     }
  38. }
  39.  
  40. function getAssetDetails(asset) {
  41.  
  42.     console.log("Asset name : " + asset.msdyn_name);
  43.     console.log("Financial Standing : " + asset.hha_financialstanding);
  44.  
  45.     var createBookingField = formContext.getControl("hha_bookingcreate");
  46.  
  47.     var optionSet = getFinancialStandingOptions("msdyn_customerasset", "hha_financialstanding", asset.hha_financialstanding);
  48.  
  49.     //var financialstanding = optionSet.find(data => data.value === asset.hha_financialstanding);
  50.  
  51.     var fsOption = optionSet.find(function (element) {
  52.         return element.value === asset.hha_financialstanding.value;
  53.     });
  54.  
  55.     return fsOption.Label.LocalizedLabels[0].Label;
  56.  
  57. }
  58.  
  59. function getFinancialStandingOptions(entityName, fieldName, optionId) {
  60.  
  61.     var optionSet
  62.  
  63.     var qryStr = "/api/data/v8.2/EntityDefinitions(LogicalName='" + entityName + "')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$filter=LogicalName eq '" + fieldName + "'&$expand=OptionSet,GlobalOptionSet"
  64.  
  65.     var req = new XMLHttpRequest();
  66.     req.open("GET", Xrm.Page.context.getClientUrl() + qryStr, false);
  67.     req.setRequestHeader("OData-MaxVersion", "4.0");
  68.     req.setRequestHeader("OData-Version", "4.0");
  69.     req.setRequestHeader("Accept", "application/json");
  70.     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  71.     req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
  72.     req.onreadystatechange = function () {
  73.         if (this.readyState === 4) {
  74.             req.onreadystatechange = null;
  75.  
  76.             if (this.status === 200) {
  77.                 var results = JSON.parse(this.response);
  78.  
  79.                 if (results === undefined || results.length == 0) {
  80.                     console.log("Option '" + fieldName + "' Set Not Found in Entity '" + entityName + "'")
  81.                 } else {
  82.                     optionSet = results.value[0].GlobalOptionSet.Options
  83.                 }
  84.  
  85.             } else {
  86.                 console.log(this.statusText)
  87.             }
  88.         }
  89.     };
  90.     req.send();
  91.  
  92.     return optionSet;
  93.  
  94. }
  95.  
  96. function errorHandler(error) {
  97.     console.log(error.message);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement