Wednesday, January 25, 2017

getEventArgs().getSaveMode() does not work for send email

getSaveMode() does not display the correct value

So I'm working on a customer who wants to perform some logic before sending an email. We used to hook onto the save event on the email form to perform this, but using global variables to store whether the user had been prompted earlier and doing several look ups made the form too slow.
PS: Please vote on ideas to change this https://ideas.dynamics.com/ideas/dynamics-crm/ID0000842

I looked into the details for the save event, and found this bit from the SDK which explains the different save modes used.

To get this object, all you need to do is check the box for "pass execution context as the first parameter" and you're good to go. The code I'm using includes something like this (don't worry, the actual code uses namespaces)

var performSaveActions = function(context) {
    if (context.getEventArgs().getSaveMode() === 7) {
        console.log("This never executes");
};

This looks OK, right? Well sadly it doesn't. Even though the SDK clearly states that when sending an email the getSaveMode() method should return 7, but it doesn't, it returns 1.

More specifically, when you hit the send button, the function executes 2 times, one time to save the content of the record, and one time before actually sending it. So I put a breakpoint in the code just to check the event arguments, and both of the times the breakpoint hits the getSaveMode() method returns 1, and never a 7.
So I tested for autosave, but that one actually returns 70, so that works as expected. I tried getting help from the different community channels, but it seems like this is a bug that hasn't gotten any attention yet.

So that's it really, if you're here looking for the same answer I was looking for then I'm afraid you're out of luck this time. What I ended up doing was hooking onto the normal save event (getSaveMode() === 1).

Also, I prevented autosave from triggering on the email form. There is a bug with autosave on the email form which can be quite annoying. If you've triggered a change on the email form and then start editing the email text field, when autosave triggers it will not include the text you've written since the dirty bit was triggered. Not only that, when the autosave is finished the text box will refresh and everything you've written will disappear.

So I implemented this snippet in the same method which stops autosave from happening (relax, this will present the user with a warning when leaving or refreshing the form if the changes haven't been saved):

if (context.getEventArgs().getSaveMode() === 70) {
    context.getEventArgs().preventDefault();
    return;
}

1 comment:

  1. Thanks for saving me a couple hours of troubleshooting!
    Also should include a link to your Ideas Post as well.:
    https://ideas.dynamics.com/ideas/dynamics-crm/ID0000842

    ReplyDelete