Jscript validation for Activate/Deactivate record in Microsoft Dynamics CRM 2011

Posted: November 6, 2012 in CRM 2011, Jscripts

To add a jscript validation for Activate or Deactivate record in CRM, everyone might probably know that we can pass the execution context as the parameter and add the validation code as below:

function OnSave(context) {
    if (context != null && context.getEventArgs() != null) {
        var saveMode = context.getEventArgs().getSaveMode();
        if (saveMode == "5") {
            // Add Deactivate validation code here
            alert("Deactivate Event");
        }
        else if (saveMode == "6") {
            // Add Activate validation code here
            alert("Activate Event");
        }
    }
}

If you observe the above two pictures, the alert message is triggered after the OOB “Confirm Contact Deactivation” message from CRM.

In one of my requirement, i need to add jscript validation before I get the OOB Confirm deactivation message. So to achieve that, first we need to know what function is called on the “Deactivate” ribbon button in CRM.

In sdk folder you can get all the CRM ribbon xml files under sdk\resources\exportedribbonxml location. In my case i need contactribbon xml file. If you open that file using visual studio, we can get the function name of deactivate button in ribbon button Command.

The command name is “Mscrm.Form.Deactivate” and jscript function name is “changeState”

Now create a new solution and add the “contact” entity to edit the ribbon customization.

Export the solution and open the customization xml file. Go to <RibbonDiffXml> and Add the following code under <CommandDefinitions> (Copy the CommandDefinition from the sdk).

Replace the function name and Library as shown above with your own file and import the solution into CRM and publish it.

Before importing the solution make sure the function and library already exists.

You can add your business required validation and call the “changeState” function to trigger OOB Deactivate record. Below is the jscript code :

function DeactivateContact(btnType, typeCode, value) {
    if (Xrm.Page.getAttribute("parentcustomerid").getValue() == null) {
        alert("Please add parent customer");
        return false;
    }
    else {
        changeState(btnType, typeCode, value);
    }
}

From now your custom validation logic will trigger first and then you can call the OOB Deactivate function method (changeState) to show the confirm message as below.

In the same way you can add the custom logic for “Activate” or for any other ribbon button by copying the CommandDefinition from sdk

Advertisements
Comments
  1. Ken Lolley says:

    Awesome info and nicely written. Keep up the fantastic stuff!

  2. Excellent blog here! Also your website loads upp very fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as fast as yours lol

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s