Stopping and continuing a save event

For some customizations you do need to stop the form onsave event, perform some business logic and continue the save event. An example would be that in specific conditions are met when a record is saved, then a popup will need to be shown. After filling in data in the popup and pressing a continue button on the popup, then the save operation would need to continue. The SDK helps in this situation. Look at the following page for more information. MSDN SDK: OnSave Event

On that page you will see that you can stop the onsave event by setting the 'event.returnValue = false'. Don't forget to follow that line with a 'return false'. This will cause the save procedure to stop right at that point. Otherwise statements after that will still be executed.

To call the save event from your javascript code, you can use the javascript functions crmForm.Save(); and crmForm.SaveAndClose(). By looking at the 'event.Mode' you can determine which event was executed before. If the code is 1, then it is a crmForm.Save(); or it is 2 for a crmForm.SaveAndClose(). There's only one small issue. There can be other save events as well. There's the 'save and new', 'save as completed', and also the 'send' for emails. Below is a list of save events with the corresponding javascript functions to call. Once again, this is not documented in the SDK, so this might change with a hotfix or new version.


Save
Code: 1
Function: crmForm.Save();


SaveAndClose
Code: 2
Function: crmForm.SaveAndClose();


Send
Code: 7
Function: send();


SaveAsCompleted
Code: 58
Function: SaveAsCompleted();


SaveAndNew
Code: 59
Function: crmForm.SubmitCrmForm(59, true, true, false);

Good luck!

5 comments:

Sanjay Munjal said...

One has to be careful as crmForm.Save() and crmForm.SaveAndClose() will call pending validation for required fields and may surprise you in your scripts. I use normally crmForm.IsDirty and FormType to ensure that I don't have surprises and my scripts work as intended.
Otherwise great work.
Thanks.

Anonymous said...

ping back from http://www.castorsoft.com/articles/2008.3.10.htm

Anonymous said...

Hi!

how to find Cancel method on Case Form.

too many controls on save event. if user Cancel case,save event javascript are runing. !!

please help me

Tudorian said...

Hi,

Great Post it has been very helpful. One Question: I have some dynamic picklist on my CrmForm , which are populated when the form loads, Do you know how to eliminate the message Your changes have not been saved... when you try to close the form?

Regards

Ronald Lemmen said...

That is a good question Tudorian. The reason why you get this message, is because CRM recognizes that one of the values of CRM has been changed.

To solve this you will need to make sure that the value which was selected when opening the form, is selected when the form closes as well (as long as the user didn't change the picklist value). This you can do by storing the initial value in the form onload and in the onblur event reset that value. This is an event which isn't described in the SDK, but there are possibilities to write to that event. I don't have a code example available, but you might be able to find one on the internet or build it yourself. If you do find/build it, please share the code here so that other users can learn from that as well.

Kind regards,
Ronald