Problem with Alert.js confirmation

Feb 12, 2016 at 2:48 PM
Hi there,

I am using the "Alert.js" pluggin to confirm the save of a Case. I expected the page to wait for the result of the Alert.js dialog, but the page continues and thus saves the Case before the user can address the confirmation.

Anyone have any experience with this?

Below is my code.
if ( duplicateRequestWithoutPRI ) {
  //Otherwise we need to give the USS the option to create or not
  Alert.show( "An active request for the same security classification for an individual with the same Lastname/DOB already exists.", null,
  [{
    label: "Create Request",
    callback: function () {
    Alert.show( "Request created successfully!", null, null, "SUCCESS", 500, 200 );
   }
  },
  {
    label: "Cancel Request",
    callback: function () {
    context.getEventArgs().preventDefault();
    }
  }], "WARNING", 500, 200 );
}
Mar 7, 2016 at 3:13 PM
Your understanding of Callbacks in JS isn't quite right. The entire save function will complete before the callback function on the button will ever get triggered. You'll need to always cancel it first, and then allow it on a new Save call from within your callback:
if ( duplicateRequestWithoutPRI ) {
  // Prevent save unless user has allowed it...
  if(someStaticVariableAllowSave){
   // Allow Save, but reset variable
   someStaticVariableAllowSave = false;
   return;
  } else {
    context.getEventArgs().preventDefault();  
  }
  //Otherwise we need to give the USS the option to create or not
  Alert.show( "An active request for the same security classification for an individual with the same Lastname/DOB already exists.", null,
  [{
    label: "Create Request",
    callback: function () {
    someStaticVariableAllowSave = true;
    Xrm.Page.data.save();
    Alert.show( "Request created successfully!", null, null, "SUCCESS", 500, 200 );
   }
  },
  {
    label: "Cancel Request",
    callback: function () {
    }
  }], "WARNING", 500, 200 );
}
Marked as answer by Liger on 7/5/2016 at 3:51 PM