One of my current projects required me to create a new Entity record when the user pressed a button that had been added to the toolbar. The two Entities were related so data is automatically copied from the Parent record to the new Child record.
The only problem I ran into was in making sure that the Parent record's data was saved before the new Child record was created so that all of the data is mapped correctly.
Background and Requirements
You can use the crmForm.Save() method to save the current record. However, if the user presses the toolbar button before completing all of the required fields, an error message will be displayed and the Save will not complete. I needed a way to ensure that all required fields were saved before creating the new Child record.
Solution
I added a new global variable to the form's OnLoad event:
SaveHasCompleted = false;
In the form's OnSave event, I added:
SaveHasCompleted = true;
I found something quite interesting while developing this solution: The OnSave event is not actually executed unless all Required fields have been completed. Or in other words, if the user receives the "please complete…" message because a Required field is blank, the Save operation will be canceled and the OnSave event never fired.
Setting SaveHasCompleted to true within the OnSave event will ensure that the record has been successfully saved to the database.
The code behind the toolbar button contains the following statement:
1: If (SaveHasCompleted)
2: {
3: // do the work here
4:
5: // Reset the flag for the next iteration
6: SaveHasCompleted = false;
7: }
Final Note
It is important that you do not preface SaveHasCompleted with the word var. This would make the variable local to the OnLoad event and not visible within the OnSave event.





