I’ve been doing this for so many times, I thought I’d share it with the new CRM customizers out there, as it’s a neat way of getting something done without delving too deep into development and plugins.
This approach uses JScript to capture the Save event on a form, and to divert it on a different course of action.
First off, let’s begin with the basics. A condition on the form will triggers this action. In most cases I want to allows the form save, but in special circumstances I want to block it and perform a different action. We’ll be doing this on the Contact form.
So let’s go ahead and add a Two Options field on the form. I named it “new_isspecialcustomer”.
The following function, associated with the OnSave event, checks for the value on my newly created field, and decides on the action.
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!");
Good. I assume you know by now how to create a web resource, add the JScript function to it, etc…
So, the preventDefault() stops the Save. What the user sees is an alert like this:
and the form remains opened and unsaved.
As presented previously in another blog post (this one to be more precise), you can write a function as such to start a workflow:
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
Now it’s starting to take shape. Now all we have to do is call this function from our previous one. Where you prevent the save, add a call to launchWorkflow. The parameters are as follows:
· GUID of the Workflow or Dialog
· The type name of the entity
· The ID of the record
And voila! Now you hijacked the save, performed your own actions, and didn’t have to fire up Visual Studio to write a plugin.
Just as a note, it’s probably more efficient to do this in a plugin, but I use this approach for a lot of quick demos I put together in a relatively short time. Gives a new meaning to the “Quick and Dirty” phrase.