Search code examples
javascripthtmldynamics-crm

Adding HTML Button to Dynamics CRM 2016 Form


What I'm attempting to do is add an HTML button that will trigger as really simple javascript function. Essentially onclick, I want to see if a field contains a value of 0.00 - if so remove that value. Or, if the field does not contain data, add in the value of 0.00 so it should alternate between those two values.

    <html>
    <head>
    </head>
    <body>
    <button onclick="ReCalc">Re-Calculate Balance</button>
      <script>
      function ReCalc() {
        var BalanceWriteOff = Xrm.Page.getAttribute("jucy_balancewriteoff").getValue();
        if ((BalanceWriteOff) ==null)
          Xrm.Page.getAttribute("balancewriteoff").setValue("0");
          Xrm.Page.data.entity.save();
        if ((BalanceWriteOff) =="0")
          Xrm.Page.getAttribute("jucy_balancewriteoff").setValue(null);
          Xrm.Page.data.entity.save();
            return;
    }
    </script>
    </body>
    </html>   

When I try to run this on the form where the HTML element has been placed. Nothing is happening. I've thrown in some break points at the var and both if statements and I'm not getting a break when I'm triggering the onclick event.

I'm kind of stumped here. If anyone has any insights for me that would be awesome


Solution

  • To access CRM form fields from an HTML web resource, add this script to the HTML:

    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
    

    and prepend "parent" to the Xrm.Page object:

    parent.Xrm.Page.getAttribute("jucy_balancewriteoff").getValue();