Skip to content

Instantly share code, notes, and snippets.

@mattruma
Created April 18, 2023 17:45
Show Gist options
  • Save mattruma/02d9b9f3c82c61ac3911bf36ab2f4399 to your computer and use it in GitHub Desktop.
Save mattruma/02d9b9f3c82c61ac3911bf36ab2f4399 to your computer and use it in GitHub Desktop.
Adventures with Power Platform – Short Circuit Model-Driven App formOnSave Event
// https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/walkthrough-write-your-first-client-script
var ItemWithStateForm = window.ItemWithStateForm || {};
(function () {
console.log("ItemWithStateForm - Version", "2023.04.18.06");
const preparingState = 968050000;
const preparingMessage = "This record is still being prepared. You won't be able to make any changes. Please try again in a few minutes.";
this.formOnLoad = function (executionContext) {
var formContext = executionContext.getFormContext();
if (!isNewRecord(formContext) && isPreparingRecord(formContext)) {
Xrm.Navigation.openAlertDialog({ text: preparingMessage });
formContext.ui.setFormNotification(preparingMessage, "INFO", "ItemWithStateForm");
}
}
this.formOnSave = function (executionContext) {
var formContext = executionContext.getFormContext();
if (!isNewRecord(formContext) && isPreparingRecord(formContext)) {
executionContext.getEventArgs().preventDefault();
Xrm.Navigation.openAlertDialog({ text: preparingMessage });
}
}
function isNewRecord(formContext) {
var entityReference = formContext.data.entity.getEntityReference();
return !entityReference.id;
}
function isPreparingRecord(formContext) {
var state = formContext.data.entity.attributes.get("maruma_state").getValue();
if (state == preparingState) {
return true;
}
return false;
}
}).call(ItemWithStateForm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment