You are currently browsing the tag archive for the ‘Stop’ tag.

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.

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();

}

}

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:

clip_image002

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
  window.location.reload(true);
}

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.

Enjoy!

Advertisements
MVP Reconnect

Check out my course [Video]

Dynamics 365 Customer Engagement Administration

Dynamics 365 Customer Engagement Administration

Check out my course [Video]

Configuring and Extending Dynamics 365 Customer Engagement

Configuring and Extending Dynamics 365 Customer Engagement

Check out my course [Video]

Getting Started with Dynamics 365 Customer Engagement

Reviewed Book

Implementing Microsoft Dynamics 365 for Finance and Operations

Implementing Microsoft Dynamics 365 for Finance and Operations

Reviewed Book

Microsoft Dynamics 365 Extensions Cookbook

Microsoft Dynamics 365 Extensions Cookbook

Check out my Book

Microsoft Dynamics CRM 2016 Customization - Second Edition

Microsoft Dynamics CRM 2016 Customization - Second Edition

Check out my Book

Microsoft Dynamics CRM Customization Essentials

Microsoft Dynamics CRM Customization Essentials

Check out my Book

Microsoft Dynamics CRM 2011 Scripting Cookbook

Microsoft Dynamics CRM 2011 Scripting Cookbook

Reviewed Book

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 608 other followers

Follow Dynamics 365 Wizardry on WordPress.com