Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Error in Javascript

Posted on by Microsoft Employee

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

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Error in Javascript

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

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Error in Javascript

    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
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Error in Javascript

    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

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans