Problem Statement:

I have validations on Form Level. Need to show Error Form notifications to users if any validation fails and prevent the Save Operation.

This is working perfect on “Save” button Click.

But, “Save and Close” button does not stopping Save operation instead showing Error Notification and closing Form.

Work around:

I have gone through many blogs but no solution found. Seems, its a Product limitation.

While researching on this issue, I got to know below workaround for this.

1. Open Entity from Ribbon Workbench

2. Customize “Save & Close” button command

3. Create below function in JS web resource and call this from “Save & Close” button instead of OOB function.

function OnSaveAndClose(executionContext) {
     Xrm.Page.data.save().then(function () {
         Xrm.Page.ui.close();
     }, function (error) {

    });
}

4. Above code, we are saving first and Closing form after successful save.

5. If any Validations fails, Save function is doing “executionContext.getEventArgs().preventDefault()”. By this, Save & Close function does not go inside Success call back so form will not close.

If all validations pass and save function executes successfully then only form will be close from Success callback.