Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Save & close button not working . it only saves the record but does not close the form.

(0) ShareShare
ReportReport
Posted on by 255

Hi All,

I have below code on the OnSave() event of form. i have added below code

Ad_OnSave: function (executionContext) {

formContext = executionContext.getFormContext();
debugger;
var currentPreferredAddFlag = formContext.getAttribute("rcm_preferredaddressflag").getValue();
if (!SaveConfirmDialog) {
var confirmStrings;
executionContext.getEventArgs().preventDefault();
if (currentPreferredAddFlag == true) {
confirmStrings = { text: "The Preferred address Flag = Yes. Do you want to continue?", confirmButtonLabel: "Yes", cancelButtonLabel: "Cancel"};
}
else {
confirmStrings = { text: "The Preferred address Flag = No. Do you want to continue?", confirmButtonLabel: "Yes", cancelButtonLabel: "Cancel"};
}
confirmOptions = { height: 100, width: 200 };
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
function (success) {
if (success.confirmed) {
//Do something on click of OK.
formContext.data.entity.save();
SaveConfirmDialog = true;
}
else {
SaveConfirmDialog = false;
}
});
}
SaveConfirmDialog = false;

},

After Adding this code its working for save button to save the record. but for Save and close button It doesnt work its saving the record but not closing the form.

Can anybody suggest what is the problem?.

  • Diana Birkelbach Profile Picture
    140 Most Valuable Professional on at
    RE: Save & close button not working . it only saves the record but does not close the form.

    What about something like this (or similar. not going into details)

    Ad_OnSave: function (executionContext) {
    
        formContext = executionContext.getFormContext();
        if (!SaveConfirmDialog) {
           executionContext.getEventArgs().preventDefault();
           //this one would be your function for the confirm dialog, returning the promise
           openConfirmDialog().then(function(success){
              if (success.confirmed) {
                 SaveConfirmDialog = true;
                 formContext.data.entity.save();
              }
              else{
                 SaveConfirmDialog = false;
                }
           });
        }
        SaveConfirmDialog = false;
    }

    While the ribbon button for save&Close would be something like this

    function mySaveAndClose(){
       //first call the confirmation
        openConfirmDialog().then(function(success){
            if(success.confirmed){
                SaveConfirmDialog = true;
                formContext.data.entity.save("saveandclose"); //it won't make the confirmation again in onSave, so no preventDefault
            }
        });
    }

    But besides that, is pretty dangerous to work with that type of confirmations in OnSave, since several saves triggered in parallel by the code (maybe in combination with button), or in case autosave is started, it may mess up your flags.

    Maybe is better to work with a boolean dummy attribute on the form, set in on required, and set in on empty if the confirmation wasn't made. This way the form stops saving by itself until the confirmation is done (and the dummy field is set).

    Or maybe try to make use of field-level notification, since it stops the form save too.

    Kind regards,

    Diana

  • Mona Chavan Profile Picture
    255 on at
    RE: Save & close button not working . it only saves the record but does not close the form.

    Hi Ju

    If i use second approach then it closes the form in both the cases For ¨Save¨ as well as for ¨Save and Close¨.

    I only want form to be close in case of Save and Close.

  • Mona Chavan Profile Picture
    255 on at
    RE: Save & close button not working . it only saves the record but does not close the form.

    Hi Ju.

    The first approach u said is to use : formContext.data.entity.save("saveandclose");

    that means instead of  formContext.data.entity.save() i need to use formContext.data.entity.save("saveandclose");

    right?

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Save & close button not working . it only saves the record but does not close the form.

    Hi Mona,

    You use preventDefault() to Disable auto-save for the form in the code, By this, Save & Close function does not go inside Success call back so form will not close.

    So you can use js function to save and close the form:

    formContext.data.entity.save("saveandclose");

    Or you can try to test with following:

    you can Go Ribbon workbench to customize “Save & Close” button command, Then 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) {
        });
    }

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans