Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

GetSaveMode

Posted on by 688

Good morning everyone,

I need to pick up the Mark As Complete From Commitment button event and I'm using the code below:

Function preventAutoSave (econtext)
{
     Var eventArgs = ExecutionContextObj.getEventArgs ();
Alert (eventArgs);

     If (eventArgs.getSaveMode () == 58)
{
Alert(teste);
     }
}

But it is giving right error and is not displaying the alert with the event number.

I'm using CRM OnLine I do not know if this code was made for ONLINE

*This post is locked for comments

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: GetSaveMode

    If you got the answer, please close the thread.

  • RE: GetSaveMode

    Tank You for your response

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: GetSaveMode

    Hi Ricardo,

    If you are using IE, it will show this error. Please try the following code. 

    var event = context.getEventArgs();
    if (event.preventDefault) {
        event.preventDefault();
    }
    else {
        event.returnValue = false;
    }

    Refer the link below for more information.

    http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie


  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: GetSaveMode

    Ok you should take different approach - you should make a separate function that calls the web service (so all your logic that is under XrmServiceToolkit.Rest.RetrieveMultiple method). This function should return a value, for example it should return true if you want to prevent the default behaviour. Now you should call this function and if it returned true you should call preventDefault on your eventArgs.

    If what I wrote is not clear for you, you should learn a little bit more about Javascript. There are plenty of tutorials on the web. This forum is not a tutorial page how Javascript works and your problems are not CRM related.

  • RE: GetSaveMode

    Thanks for the answer,

    I am getting the following error message in debug

    5824.Sem-t_ED00_tulo.jpg

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: GetSaveMode

    I will just paste a part of the code - at the end of your code you have:

    //On Complete - Do Something

    }, true);

    change it to:

    //On Complete - Do Something

    }, false);

  • RE: GetSaveMode

    Thanks for the answer, I did not understand, could you give me an example based on my code?

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: GetSaveMode

    It does not work because you are using asynchronous callbacks, so your context is may be different from OnSave context (and only OnSave event context contains preventDefault). You should change the last "true", to "false" to call the data synchronously, so you will be still in the proper context when running a callback.

  • RE: GetSaveMode

    function preventAutoSave(context)

    {

    debugger;

    if(context.getEventArgs().getSaveMode() == 58)

    {

    var evento = context.getEventArgs().getSaveMode();

    if(Xrm.Page.getAttribute("customerid").getValue() != null)

    {

    var ClienteId = Xrm.Page.getAttribute("customerid").getValue()[0].id;

    var Subject = Xrm.Page.getAttribute("subject").getValue();

    XrmServiceToolkit.Rest.RetrieveMultiple("AccountSet", "?$select=AccountNumber,Name&$filter=AccountId eq guid'" + ClienteId + "'", function(results)

    {

    for (var i = 0; i < results.length; i++)

    {

    var accountNumber = results[i].AccountNumber;

    var name = results[i].Name;

    }

    if(Subject == "X" && accountNumber == null)

    {

    context.getEventArgs().preventDefault();

    }

    },

    function(error)

    {

    Xrm.Utility.alertDialog(error.message);

    }, function()

    {

    //On Complete - Do Something

    }, true);

    }

    }

    }

  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: GetSaveMode

    Please share your code

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

Featured topics

Product updates

Dynamics 365 release plans