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 CRM (Archived)

Error in Javascript

(0) ShareShare
ReportReport
Posted on by

Hi!

Before the user leaves the form (Contact), either closes the form, close the window (web browser) or clicks in the menu I need to save some data to the entity.

My script is called from the OnLoad event of the form and it looks like this:

function SaveBeforeClosingForm() 
{
  window.onbeforeunload = function() 
  {
    Xrm.Page.getAttribute("new_optionalstatus").setValue(100000001);
    Xrm.Page.data.entity.save();
    return null; 
  }
}

It works fine the first time I open the form and closes it.
When I open it again I can see my change have been saved but when I try to close it the second time I get a Javascript error.
I don't see anything wrong in my code, it works the first time.

Any help out there?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Hi

    Using DOM and event handling for the DOM events (Example: window.onbeforeunload) is not supported.

    Unsupported customization is not recommended.

    Please refer the following:

    gustafwesterlund.blogspot.co.uk/.../unsupported-customizations.html

  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     I think Gopalan is (just removed "somewhat" here) correct that you should not be using unsupported methods.. On the other hand, window object is always going to be there.. However, dynamics is, probably, already using onbeforeunload. You are overwriting it, so you are breaking out-of-the-box scripts (why it's working the first time and not working the second time I don't know - does not matter that much)

     You migth try to do it this way - might help:

    var defaultOnBeforeUnload = null;

    function SaveBeforeClosingForm()

    {

     defaultOnBeforeUnload  =   window.onbeforeunload;

     window.onbeforeunload = function()

     {

       Xrm.Page.getAttribute("new_optionalstatus").setValue(100000001);

       Xrm.Page.data.entity.save();

       if(defaultOnBeforeUnload  != null)

          defaultOnBeforeUnload();

       return null;

     }

    }

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    if your purpose is to set a default value for that optionset, then use a synchronous workflow or a plugin instead doing some javascript

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans