Search code examples
javascriptdynamics-crmmicrosoft-dynamicsdynamics-365dynamics-crm-365

Confirmation Dialog Box for OnCreate Event


I am trying to create a dialog box as a warning to the user when trying to create an entity record in a custom entity.

I wrote a JS which I tried to trigger onSave of form. However, this dialog box is popping up in the loop. This is happening both when I accept or cancel the event.

function OnFormSave(executionObj) {
    var eventArgs = executionObj.getEventArgs();
    var formContext = executionObj.getFormContext();
  //  eventArgs.preventDefault();

    if (eventArgs.getSaveMode() == 70)//AUTOSAVE
    {
        eventArgs.preventDefault();
        return;
    }

    var confirmStrings = { text: "Are you sure you want to continue to save the record?", title: "Warning" };
    var confirmOptions = { height: 250, width: 500 };
    Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    function (success) {
        if (success.confirmed)
        {
           // formContext.data.save();
        }
        else {
            eventArgs.preventDefault();
          
        }
    });
}

Can someone help me with this? Also, this event should happen only when the record is create. Any fix for that?


Solution

  • I tried to answer this in a little different way:

    var count = 0;
    
    function OnFormSave(executionObj, para) {
        var eventArgs = executionObj.getEventArgs();
        var formContext = executionObj.getFormContext();
    
        if (count == 0) {
            eventArgs.preventDefault();
        }
    
        if (formContext.ui.getFormType() == 1 && para != null && count < 1) {
    
    
            var confirmStrings = { text: "Are you sure you want to continue to save the record?", title: "Warning" };
            var confirmOptions = { height: 250, width: 500 };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
                function (success) {
                    if (success.confirmed) {
                        count = count + 1;
                        formContext.data.save();
                    }
                    else {
                        eventArgs.preventDefault();
    
                    }
                });
        }
    }