This project has moved. For the latest updates, please go here.
1

Closed

Alert.js Not Working In ONSAVE Method

description

hi. I use Microsoft Dynamic CRM 2016 on-Premise. I Use Alert.js Lib in OnSave Method To Call and Get Response from current User That Loged into the CRM. But It Is Not Working. Please Help Me.
When Call The Alert.Show It Is Not Working After Save Process Completed It Show To Me Dialog With Two Button. I Want To Prevent Save. :(

My Code:

function Onsave(econtext) {
var NewFeature = Xrm.Page.getAttribute("eit_featuregroup").getValue();
Newstatus = Xrm.Page.getAttribute("statuscode").getValue();
if (FormType == 2) {
    if (Newstatus == 915840011 || Newstatus == 915840012) {
        NewTeam = RetrieveTeam(NewFeature[0].id);
    }
}

if (NewTeam != CurrentTeam) {
    TeamChangeconfirm(econtext);
}
}


function TeamChangeconfirm(econtext) {
Alert.show("This Function Is Go", null, [
   new Alert.Button("Yes", null, true, false),
   new Alert.Button("No", function () {
       preventSave(econtext);
       Alert.show("It Is Not Save", null, [new Alert.Button("Close", null, true, false)], "INFO", 500, 200);
   }, true, false)
], "QUESTION", 500, 200);
}

function preventSave(econtext) {
var eventArgs = econtext.getEventArgs();
var SaveMode = eventArgs.getSaveMode();
if (SaveMode == 70 || SaveMode == 2 || SaveMode == 1) {
    eventArgs.preventDefault();
}
}
Closed Dec 6, 2016 at 12:49 AM by Liger_cp <SND\Liger_cp>
Not a bug

comments

Liger wrote Dec 6, 2016 at 12:28 AM

Showing the alert won't prevent the rest of the JavaScript from firing, so your form will continue to save as normal before you get a chance to select "No" to prevent the save.

What you need to do is prevent the save before showing the alert, and then in the "Yes" function, have additional logic to trigger the save again. However, note that triggering the save will retrigger the onsave event, so you need a variable to prevent it firing twice, and displaying the alert again. E.g. before triggering the save, set a global variable which you can check in your Onsave, to prevent it firing again.

Liger wrote Dec 6, 2016 at 12:48 AM

Something like this "should" work:
var _autoFired = false;
function Onsave(econtext) {
    if (_autoFired) { _autoFired = false; return; }

    var NewFeature = Xrm.Page.getAttribute("eit_featuregroup").getValue();
    Newstatus = Xrm.Page.getAttribute("statuscode").getValue();
    if (FormType == 2) {
        if (Newstatus == 915840011 || Newstatus == 915840012) {
            NewTeam = RetrieveTeam(NewFeature[0].id);
        }
    }

    if (NewTeam != CurrentTeam) {
        TeamChangeconfirm(econtext);
    }
}


function TeamChangeconfirm(econtext) {
    preventSave(econtext);

    Alert.show("This Function Is Go", null, [
        new Alert.Button("Yes", function () {
            _autoFired = true;
            Xrm.Page.data.entity.save();
        }, true, false),
        new Alert.Button("No", function () {
            Alert.show("It Is Not Save", null, [new Alert.Button("Close", null, true, false)], "INFO", 500, 200);
        }, true, false)
    ], "QUESTION", 500, 200);
}
Regards,
Paul

wrote Dec 6, 2016 at 12:49 AM