In crm, sometime we want to override the operation being performed by oob or we want to perform action only if certain conditions are met.
In this post,i’ll share how we can override the out of the box button’s behavior.
Scenario: We have deactivate button on account record. We want that only if field named phone doesn’t contains data, then only deactivation should be allowed otherwise system should show and alert and don’t deactivate the record.
Below is step by step solution for above problem:
Step 1:
Add the account entity having that button in a solution along with a javascript library
1
Step 2:
Open that solution using ribbon workbench as shown below:
2
Step 3:
Right click on the deactivate button and click on customize command as shown below:
3.png
Step 4:
Now click on deactivate command and change the library to your javascript library i.e. “new_account”. Also copy the function being called on that command (ref 3 in below screen shot). This function will be used in out javascript function. It’s “Mscrm.CommandBarActions.changeState” in our case.
4.png
Step 5:
Replace the oob function with your function name (“overridedeactivatebtn” in our case)
7.png
Keep in mind here 3 parameters are being passed. We’ll pass these three parameters when we’ll call oob function “Mscrm.CommandBarActions.changeState” from our js fucntion.
Step 6:
Publish the solution in ribbon workbench:
6.png
Step 6:
In new_account library, overridedeactivatebtn function will have below code.
function overridedeactivatebtn(parm1,parm2,parm3)
{
  var phone= Xrm.Page.getAttribute("telephone1").getValuephone!=null)
  {
  alert("Can't deactivate account having phone number.");
  }
  else 
  {
  Mscrm.CommandBarActions.changeState(parm1,parm2,parm3);
  }
}
Publish your javascript library.
Step 7:
To test click on deactivate button on account record having value in phone number field and you’ll get the alert message and no oob pop up of deactivation will come:
8.png
Now, clear the phone field and click deactivate again. CRM’s oob deactivation pop-up will come:
9.png
So, we successfully override the  functionality of   out of the box crm button.
Hope it would be helpful..!