While working with the Form OnLoad
event can help us format and arrange the user interface, working with the Form OnSave
opens up a new door towards validation of user input and execution of business process amongst others.
Using the same solution we have worked on in the previous recipe, we will continue to demonstrate a few other aspects of working with the forms in Dynamics CRM 2011. In this recipe the focus is on the handling the Form OnSave
event.
First off, in order to kick off this, we might want to verify a set of fields for a condition, or perform a calculation based on a formula. In order to simplify this process, we can just check a simple yes/no condition on a form.
Using the previously customized solution, we will be taking advantage of the Contact entity and the fields that we have already customized on that form. If you are starting with this recipe fresh, take the following step before delving into this recipe:
Using this field, if the answer is No, we will stop the save process.
new_ch4rcp2
. Set its type to JScript.function StopSave(context) { var _isSpecialSelection = null; var _isSpecial = Xrm.Page.getAttribute("new_isspecialcustomer"); if(_isSpecial != null) { _isSpecialSelection = _isSpecial.getValue(); } if(_isSpecialSelection == false) { alert("You cannot save your record while the Customer is not a friend!"); context.getEventArgs().preventDefault(); } }
No
, we can bring up an alert and stop the Save and Close
event.OnSave
event.While this recipe only describes in a very simplistic manner the way to stop a form from saving and closing, the possibilities here are immense. Think about what you can do on form save, and what you can achieve if a condition should be met in order to allow the form to be saved.
Another good use for blocking the save and close form is to take a different path. Let's say we want to kick off a workflow when we block the save form. We can call from the previous function a new function as follows:
function launchWorkflow(dialogID, typeName, recordId) { var serverUri = Mscrm.CrmUri.create('/cs/dialog/rundialog.aspx'), window.showModalDialog(serverUri + '?DialogId=' + dialogID + '&EntityName=' + typeName + '&ObjectId=' + recordId, null, 'width=615,height=480,resizable=1,status=1,scrollbars=1'), // Reload form window.location.reload(true); }
We pass to this function the following three parameters:
3.149.254.7