Search code examples
javascripthtmlcssdynamics-crmmicrosoft-dynamics

Button on Form Not going through all JS


I've added a web resource in the shape of a button onto a form in our CRM online instance. I've been debugging for a while now and I don't understand what I'm missing here.

Below is the web resource (I've omitted the javascript URL for obvious reasons)

<html>
<head>
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
integrity="sha384-
BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" 
crossorigin="anonymous">
</head>
<body>
<button onclick="TriggerNPS()" class=".btn-default">
<i class="glyphicon glyphicon-check"></i>
Send NPS Now
</button>
<script src="Javascript URL" type="text/javascript"></script>
</body>
</html>

Javascript that the URL links to

function TriggerNPS() {
var NoTick = parent.Xrm.Page.getAttribute("ondemandnps").getValue();
if ((NoTick) ==null);
  parent.Xrm.Page.getAttribute("ondemandnps").setValue("1");
  parent.Xrm.Page.data.entity.save();
    return
if ((NoTick) !=null)
  parent.Xrm.Page.getAttribute("ondemandnps").setValue("0");
  parent.Xrm.Page.data.entity.save();
    return;

}

What I'm seeing is that if the field which this is looking at (tick box) contains data, then sure it strips the tic. However if the box is empty. I cannot get the script to add the check. Testing this in the console though the command does work. It runs over the lines and doesn't step out of them, but does nothing.

Any help will be appreciated.


Solution

  • Maybe try this:

    function TriggerNPS() {
        var NoTick = parent.Xrm.Page.getAttribute("ondemandnps").getValue();
        if (NoTick == null) {
            parent.Xrm.Page.getAttribute("ondemandnps").setValue(true);
        }
        else {
          parent.Xrm.Page.getAttribute("ondemandnps").setValue(false);
        }
        parent.Xrm.Page.data.entity.save();
    }