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...
Suggested Answer

Model-Drive-App - formContext.data.save({saveMode:2}} and formContext.data.entity.save("saveandclose") are not closing

(0) ShareShare
ReportReport
Posted on by 1,532

Hi

I have converted my solution to a model-drive-app.

I have been working with the save api's  formContext.data.save and formContext.data.entity.save.

I have run across several issues.

1) When using the close form options {saveMode:2} or "saveandclose".  The data is getting saved but the form does not close.

My work around was to use the following code

formContext.data.save().then(function() {
    formContext.ui.close();
});

2) If updating a field on the back-end when saving, if you use the following code.  The form will activate the "unsaved change" and you will get the following dialog box.

formContext.getAttribute("new_isengineerupdatealldatasheet").setSubmitMode("always")

(formContext.getAttribute("new_isengineerupdatealldatasheet")).setValue(false);

This will display this error message

4861.ProcessBar.JPG

3) Using the save API and trying to use the showProcessIndicator.  The save api's will cause this not to work.  But sometimes it may work.

Xrm.Utility.showProgressIndicator(displayMessage);

I have the same question (0)
  • Suggested answer
    Henry J. Profile Picture
    5,237 on at

    Hi,

    1) In my opinion formContext.data.entity.save(saveOption) should work.
    If it doesn't, there might be an issue elsewhere. 
    I tried Xrm.Page.data.entity.save("saveandclose"); in the browser F12 console and it works as expected: it saves the data and then closes the form (your code did work too).
    Maybe consider opening a case to help troubleshoot this: https://admin.powerplatform.microsoft.com/support

    2) Could you elaborate on what you're trying to achieve and how?
    What is the event associated with your function?

    I've never seen Xrm.Page.BooleanAttribute, so I'm not sure it's a supported method.
    You should consider removing Xrm.Page from your code and replace it with formContext, as it is deprecated.

    3) Can you also elaborate on this, maybe with more details of your code and a bit of background on what you are trying to achieve?

    Henry

  • Ics_rthompsoneu Profile Picture
    74 on at

    Hi Henry,

    Thanks for responding back.

    I am doing 2 different types of  Api save.

    1) In my opinion formContext.data.entity.save(saveOption) should work.

    My first save looks like this.  I am displaying a dialog.

    var displayMessage = "Are you sure you want to assign this case to the engineer?";
    var confirmStrings = { text: displayMessage, title: "Assign To Engineer" };
    var confirmOptions = { height: 80, width: 260 };
    Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
           function (success) {
               if (!success.confirmed) {
                   (formContext.getAttribute("new_isassigntoprojectengineer")).setValue(false);
                   return;
               }
    
                *** This is not working ***
                //formContext.data.save({saveMode: 2});
               //formContext.data.entity.save("saveandclose");
    
              formContext.data.save().then(function() {
              formContext.ui.close();
          });
       },
    );

    I tried Xrm.Page.data.entity.save("saveandclose"); in the browser F12 console and it works as expected: it saves the data and then closes the form (your code did work too).

    Just asking. Are you working with model-driven-app?

    Maybe consider opening a case to help troubleshoot this: https://admin.powerplatform.microsoft.com/support

    I did open a case spoke with Microsoft not much help.  Even show them the issue. 

    2) Could you elaborate on what you're trying to achieve and how?
    What is the event associated with your function?

    My event is triggered on "OnSave".

    I've never seen Xrm.Page.BooleanAttribute, so I'm not sure it's a supported method.

    I am using typescript (d.ts) for CRM.

    Typescript.JPG


    You should consider removing Xrm.Page from your code and replace it with formContext, as it is deprecated.

    Yes I am using "formContext" and not "Xrm.Page".

    3) Can you also elaborate on this, maybe with more details of your code and a bit of background on what you are trying to achieve?

    My second save is an event "OnSave".

    formContext.data.save().then(function () {
         addDocumentFieldToEntity(formContext);
         StartCreatingFromWebResources(formContext);
    });
    
    function StartCreatingFromWebResources(formContext: any) {
      //Not working
      Xrm.Utility.showProgressIndicator(displayMessage);
    
      //I do have a timer on this
      Xrm.Utility.closeProgressIndicator();
    });

    I was able to find what was causing the Xrm.Utility.showProgressIndicator not to work.

    I changed the code to do the following.

    var displayMessage = "Saving Data Sheet Template. Please Wait...";
    
    Xrm.Utility.showProgressIndicator(displayMessage);
    
    window.setTimeout(function () {
        /// Using Timeout of secs, to close the progress Indicator
        Xrm.Utility.closeProgressIndicator();
    },47000);
    
    setTimeout(function () {
        var caseDataSheetTemplateUpdateEntity = new CaseCommon.CaseDataSheetTemplateUpdateEntity();
        addDocumentFieldToEntity(formContext, caseDataSheetTemplateUpdateEntity);
        StartCreatingFromWebResources(formContext, caseDataSheetTemplateUpdateEntity);
    }, 1000);

  • Suggested answer
    Henry J. Profile Picture
    5,237 on at

    Thank you for the detailed answer rthompson

    Yes, I'm using model-driven apps in Unified Interface :)

    For 1), I'm not sure what goes wrong here, in my tests the client APIs work as expected, so support is the right way to go.

    For 2), if I understand correctly, you set an extra value on a field right after a Form OnSave Event is triggered on the form.
    This could lead to multiple saves, as the OnSave Event also triggers from code triggere saves (formContext.data.entity.save or formContext.data.save).
    Have you considered using the preventDefault method to manage save in your own way and prevent multiple / looping saves?

    For 3) Good to see it's fixed

  • rthompson Profile Picture
    1,532 on at

    Thanks Henry.

    I will give this preventDefault a try.

    Thanks again for your time.

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
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans