deleriumbg

Form Script

Jul 3rd, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Sdk = window.Sdk || {};
  2.  
  3. (function () {
  4.     // Code to run in the form OnSave event
  5.     this.formOnSave = function (executionContext) {
  6.         debugger;
  7.         var formContext = executionContext.getFormContext();
  8.         let customCreationDate = formContext.getAttribute("new_customcreationdate");
  9.         if (customCreationDate.getValue() == null) {
  10.             preventAutoSave(executionContext);
  11.             Xrm.Navigation.openAlertDialog({ text: "Record can not be saved." });
  12.             return;
  13.         }
  14.  
  15.         let currentDate = new Date();
  16.         let diffTime = currentDate.getTime() - customCreationDate.getValue().getTime();
  17.         let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  18.  
  19.         Xrm.WebApi.retrieveRecord("new_systemrules", "6FCCBE6E-699D-E911-A980-000D3A26C11D", "?$select=new_rulevalue").then(
  20.             function success(result) {
  21.                 const entityId = formContext.data.entity.getId().slice(1, -1);;
  22.                 let systemRuleValue = result.new_rulevalue;
  23.                 console.log("Retrieved Rule value: " + systemRuleValue);
  24.  
  25.                 if (diffDays < systemRuleValue) {
  26.                     preventAutoSave(executionContext);
  27.                     Xrm.Navigation.openAlertDialog({ text: "Record can not be saved." });
  28.                 } else {
  29.                     // Define the data to create new account
  30.                     var data =
  31.                     {
  32.                         "new_recordname": formContext.getAttribute("name").getValue() + "_" + currentDate.toLocaleDateString("en-US"),
  33.                         "new_creationdate": customCreationDate.getValue(),
  34.                         "new_deactivationdate": currentDate,
  35.                         "new_deactivationperiod": Number(systemRuleValue),
  36.                         "[email protected]": "/accounts(" + entityId + ")"
  37.                     }
  38.  
  39.                     console.log(`/accounts(${entityId})`)
  40.                     // Create Account deactivation history record
  41.                     Xrm.WebApi.createRecord("new_accountdeactivationhistory", data).then(
  42.                     function success(result) {
  43.                         console.log("Account deactivation history record created with ID: " + result.id);
  44.  
  45.                         // Set Account entity to Inactive status
  46.                         SetStateRequest(entityId);
  47.                         Xrm.Navigation.openAlertDialog({ text: "Record saved." });
  48.                     },
  49.                     function (error) {
  50.                         console.log(error.message);
  51.                     });
  52.                 }  
  53.             },
  54.             function (error) {
  55.                 console.log(error.message);
  56.             }
  57.         );  
  58.     }
  59.  
  60.     // Code to run in the VIP Account attribute OnChange event
  61.     this.vipAccountOnChange = function (executionContext) {
  62.         debugger;
  63.         var formContext = executionContext.getFormContext();
  64.         let vipAccountNameObj = formContext.getAttribute("new_vipaccount_name");
  65.         let tabObj = formContext.ui.tabs.get("SUMMARY_TAB");
  66.         let sectionObj = tabObj.sections.get("VIP_Account_Details");
  67.  
  68.         if (formContext.getAttribute("new_vipaccount").getValue() == 1) {
  69.             vipAccountNameObj.setValue(formContext.getAttribute("name").getValue());
  70.             sectionObj.setVisible(true); //to show
  71.         } else {
  72.             vipAccountNameObj.setValue("");
  73.             sectionObj.setVisible(false); //to hide
  74.         }
  75.     }
  76. }).call(Sdk);
  77.  
  78. function preventAutoSave(executionContext) {  
  79.     var eventArgs = executionContext.getEventArgs();  
  80.     if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {  
  81.         eventArgs.preventDefault();  
  82.     }  
  83. }
  84.  
  85. function SetStateRequest(entityId) {
  86.     var entity = {};
  87.     entity.statuscode = 2;
  88.     entity.statecode = 1;
  89.  
  90.     var req = new XMLHttpRequest();
  91.     req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts(" + entityId + ")", true);
  92.     req.setRequestHeader("OData-MaxVersion", "4.0");
  93.     req.setRequestHeader("OData-Version", "4.0");
  94.     req.setRequestHeader("Accept", "application/json");
  95.     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  96.     req.onreadystatechange = function () {
  97.         if (this.readyState === 4) {
  98.             req.onreadystatechange = null;
  99.             if (this.status === 204) {
  100.                 Xrm.Navigation.openAlertDialog({ text: "Record saved." });
  101.             }
  102.             else {
  103.                 Xrm.Utility.alertDialog(this.statusText);
  104.             }
  105.         }
  106.     };
  107.     req.send(JSON.stringify(entity));
  108. }
Advertisement
Add Comment
Please, Sign In to add comment