Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Save a form from a button

(0) ShareShare
ReportReport
Posted on by 385

I've looked at other posts and found some code to leverage but it doesn't do anything on my form.

I created a web resource html with the following:

<html><head>
<meta><meta><meta></head>

<body onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;">
<button onclick="SaveForm()">Save Notes</button>
<script>
function SaveForm(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)},
)
}
}
</script>

</body></html>

've added this to my form.

Button shows up fine but doesn't actually save.  Still says unsaved changes in bottom right.

Not giving me any errors either.

Any ideas?

Thanks,

Terry

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Save a form from a button

    Hi Mattw,

    As discussion in this post:

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/284186/how-to-pass-the-execution-context-to-crm-html-webresource-in-dynamics-365-v9

    Currently we could still work with parent.Xrm.Page in our web resource(in form) to call client API.

    You could instead replace formContext with parent.Xrm.Page:

    <html>
        <head>
            <meta />
            <meta />
            <meta />
        </head>
        <body onfocusout="parent.setEmailRange();"
            style='overflow-wrap:break-word;'>
            <button onclick="SaveForm()">Save Notes</button>
            <script>
          function SaveForm() {
            debugger;
            var id = parent.Xrm.Page.data.entity.getId();
            var entity = parent.Xrm.Page.data.entity.getEntityName();
              if (parent.Xrm.Page.data.entity.getIsDirty()) {
                alert("3");
              }
              parent.Xrm.Page.data.save().then(
                function() {
                    parent.Xrm.Utility.openEntityForm(entity, id);
                },
                function() {
                  alert(errorCode + message);
                }
              );
          }
        </script>
        </body>
    </html>

    Regards,

    Clofly

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

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans