Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

Posted on by 1,530

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);

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Model-Drive-App - formContext.data.save({saveMode:2}} and formContext.data.entity.save("saveandclose") are not closing

    Thanks Henry.

    I will give this preventDefault a try.

    Thanks again for your time.

  • Suggested answer
    Henry J. Profile Picture
    Henry J. 5,237 on at
    RE: Model-Drive-App - formContext.data.save({saveMode:2}} and formContext.data.entity.save("saveandclose") are not closing

    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

  • Ics_rthompsoneu Profile Picture
    Ics_rthompsoneu 70 on at
    RE: Model-Drive-App - formContext.data.save({saveMode:2}} and formContext.data.entity.save("saveandclose") are not closing

    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
    Henry J. 5,237 on at
    RE: Model-Drive-App - formContext.data.save({saveMode:2}} and formContext.data.entity.save("saveandclose") are not closing

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans