1
votes

Very new to Javascript - I have searched but found only partial answers to my issue.

I have a YES/NO option set on a CRM entity. When the user clicks YES, I want a confirmation prompt to appear with OK/CANCEL confirmation. This part works fine, but I also want the option set to return to NO if the user clicks CANCEL. I cannot figure this part out.

My code below - please could you indicate where the additional code should go which returns the option set back to NO on a CANCEL click? Thanks so much for assistance:

function new_submitforapproval_onchange ()
{
var approval = confirm("confirm message here");
if (approval)
{
  alert("ok message here");
}
else
{
  alert("cancel message here");
}
}
1
just set the value of the optionset after your alert("cancel message here"):Guido Preite
Great thanks, Xrm.Page.data.entity.attributes.get('new_fieldname').setValue(null) worked just fine in that spot.user2867601

1 Answers

2
votes

A more generic solution could be the following function:

function confirmChange(eCxt, promptMessage, okMessage, cancelMessage) {
    var promptMessage = promptMessage || "This is the default prompt";
    var okMessage = okMessage || "ok message here";
    var cancelMessage = cancelMessage || "cancel message here";
    if (confirm(promptMessage)) {
        alert(okMessage);
    } else {
        alert(cancelMessage);
        eCxt.getEventSource().setValue(0); // This assumes the field is boolean. 
        //eCxt.getEventSource().setValue(null); // Change the above line to this if applicable to an OptionSet.
    }
}

eCxt is passed to the function by ticking the "Pass execution context as first parameter" box on the event handler dialog for the Yes/No field you are applying this to.

The other 3 parameters are optional and can be set using the comma separated list field of the same event handler dialog.