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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Customer Service forum

Save and refresh form.

(0) ShareShare
ReportReport
Posted on by 244

I have written the below code inorder to save and refresh form. Can someone help me with the solution to achieve this?
function OnSave(executionContext) {
debugger;
var formContext = executionContext.getFormContext();
var id = formContext.data.entity.getId();
var entity = formContext.data.entity.getEntityName();
if (executionContext != null) {
if (formContext.data.entity.getIsDirty()) {
alert("3");
if (formContext.data.save().then(function () { {
alert("2j");
}
Xrm.Utility.openEntityForm(entity, id);
}, null));

}
}
}

Categories:
I have the same question (0)
  • NK Bhairi Profile Picture
    244 on at

    I am calling this function on onsave of form as auto save disabled.

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi NK,

    Did you use my code instead of yours? Could you kindly share your complete requirement and code please?

    Best Regards

    Leo

  • NK Bhairi Profile Picture
    244 on at

    No luck with this as well! on save it is directly going out of loop.

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi NK,

    I improved the code and it worked well. Now we can successfully save and refresh the form without the warning pop.

    function OnSave1(executionContext) {
        debugger;
        var formContext = executionContext.getFormContext();
        var id = formContext.data.entity.getId();
        var entity = formContext.data.entity.getEntityName();
        if (executionContext != null) {
            if (formContext.data.entity.getIsDirty()) {
                alert("3");
               
            }
            //formContext.data.entity.save();
            formContext.data.save().then(
                function () {Xrm.Utility.openEntityForm(entity, id);},
                function () {alert(errorCode+message)},
            )
            
        }
    }


    Hope it helps.

    Best Regards

    Leo

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi NK,

    I met the same condition too, you could ignore this prompt and click ok and then the form will refresh and the entity will be updated.

  • NK Bhairi Profile Picture
    244 on at

    This is the function I am calling onChange of fieldtext attribute. But the Save function is not working and it always shows me a pop up

    Your changes have not been saved. To stay on the page so that you can save your changes, click Cancel.

    function savefields(executionContext, fieldtext, setfieldtext) {

    var formContext = executionContext.getFormContext();

    var id = formContext.data.entity.getId();

               var entity = formContext.data.entity.getEntityName();

       if (executionContext != null) {

           if (formContext.getAttribute(fieldtext).getValue() != null) {

               formContext.getAttribute(setfieldtext).setValue(1);

    Xrm.Page.getAttribute(setfieldtext).setSubmitMode("always");

               formContext.data.entity.save();

    //Xrm.Page.data.entity.refresh();

    Xrm.Utility.openEntityForm(entity, id);

           }

       }

    }

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi NK,

    Please try the following code.

    function OnSave(executionContext) {
        debugger;
        var formContext = executionContext.getFormContext();
        var id = formContext.data.entity.getId();
        var entity = formContext.data.entity.getEntityName();
        if (executionContext != null) {
            if (formContext.data.entity.getIsDirty()) {
                alert("3");
               
            }
            formContext.data.entity.save();
            Xrm.Utility.openEntityForm(entity, id);
        }
    }

    I have tried this in my environment and it worked well.

    According to Microsoft SDK, You should use "formContext.data.entity.save" instead of "formContext.data.save";

    Commun1.PNG

    In addition, don't forget to select "Pass execution context as first parameter" when adding functions.

    Commun1.PNG

    Hope it helps.

    Best Regards

    Leo

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You need to share more details on your requirement. What do you exactly want and when do you want to trigger the script, if you have already registered this script then on which event etc.

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Customer Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans