web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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?.

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    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.

  • Mona Chavan Profile Picture
    255 on at

    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?

  • Mona Chavan Profile Picture
    255 on at

    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.

  • Diana Birkelbach Profile Picture
    140 Most Valuable Professional on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 46 Most Valuable Professional

#2
Pallavi Phade Profile Picture

Pallavi Phade 33

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 28 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans