One of the most common requirements that you will eventually encounter is to block a form from getting saved if a certain condition is not met.
Use any of the previously created solutions, or if starting at this recipe create a new solution for this exercise. Add the Contact entity to your solution.
To block events, perform the following steps:
Can Save
(new_cansave
) and leave the default Yes and No values.new_JSEventBlocking
).function BlockSave(context) { var _canSave = Xrm.Page.getAttribute("new_cansave").getSelectedOption().text; if(_canSave == "No") { Xrm.Page.context.getEventArgs().preventDefault(); } }
k
Save function to the form OnSave
event. Make sure you check the checkbox for Pass execution context as first parameter.In this example the rule is as simplistic as it can get. We read the value of Can Save, and if the value is No, then we block the form from getting saved. In a real-life situation your condition will definitely be more complex than that. It could be driven by a set of fields, a complex calculation, or any other business rule. For simplicity, you can always create a hidden field similar to our Can Save field, and based on the business rules set the value accordingly. Then your script to prevent a save is kept clear of your main logic.
The opposite of stopping a form from getting saved has a few variations. We can look at either the simple straightforward Save, Save and New, or Save and Close.
In order to force a save in JavaScript, incorporate the following line of code in a function that you can call when needed:
Xrm.Page.data.entity.save();
In Update Rollup 8 for Dynamics CRM 2011, an issue was fixed that was preventing using scripting to cancel the OnSave
event in a recurring appointment. This is documented with the release notes. Additional details are found on the Update Rollup 8 page at http://support.microsoft.com/kb/2600644.
18.218.123.227