Add Validation or Custom Script to OOB Buttons on CRM forms

科技动态 2018-06-25 阅读原文

Hello Everyone,

Recently I had a requirement to add in a validation logic before I Close the Opportunity as Won or Lost. The logic was simply i.e. to make sure the Est Close Date is not left empty; rather set to a date value before closure for better reporting and analysis purpose.

The best and recommended approach to do this is hide the OOB button and Add the same functionality to a new button with your embedded validation script. To hide and copy the OOB button functionality I used Ribbon Workbench. I would recommend to watch all the video series posted by Scott Durow himself on Ribbon Workbench here
before proceeding to have a clear understanding of the tool.

To start with, first I would create a new Solution which will have only the Opportunity Entity and the new WebResource. This is required for the Ribbon Workbench to populate only the opportunity ribbons to edit.

Next, open the new XRMToolBox and connect to the Ribbon Workbench 2016.

Create a new connection to your Organization.

Once the connection to your CRM Organization is successful, select the newly created Solution and click OK.

The Ribbon Workbench Tool will download the solution and open all the opportunity ribbons in their own format to be edited.

Since we need to apply the validation on the OOB buttons Close as Won and Close as Lost. Hence we first make a copy of the OOB button so we don’t touch and keep the original customization as a backup. Note that the OOB button properties are grayed out.

After copying the OOB button, right click the same group and select Paste.

A new button with the same name but a different ID would be created. Notice that this new button Control properties are not grayed-out and open for editing.

Just now only the button properties are editable. To update the commands section, right click on the same button and click on ‘ Customise Command

This will open the Command properties to be edited. Since the commands ID is still the same for the new button hence we would take a copy and work on it; without touching the original else the OOB buttons commands will also be updated.

Likewise, paste the same under the command section.

Notice that a new command with a different ID would be created. You can Update the ID of the command if you wish to more meaningful.

Browse to the new button and update the command to the latest copied one.

Also update the Command Core properties.

Once updated, now we can proceed to change the copied command attached to the new button now. Browse to the command, notice that a Custom Javascript Action is present with
Library, Function Name and a Boolean Parameter. Take of note of these as we would need this back again in our custom webresource later.

Since we need to attach our own webresource, hence we would delete the already existing one by right click and Delete
on the Action Tab.

Now click on Add Actions and select the option Javascript Action

Search for your newly created webresource and add in the function name. For now this function only has the alert to make sure the function is called on the new button click.

Once Done, Publish the changes.

After publishing, refresh your CRM Organization and Open the Opportunity. Notice the 2 Close as Won buttons on the ribbon (One copied and another OOB). Click on the copied button and my alert works! :grinning:

Now to make sure the OOB button functionality is not updated and remains the same, click on it and the Close Opportunity window will pop-up.

Once the button functionalities are verified for both the copied and OOB buttons. Now it’s time to update the webresource with the exact requirement.


If the Est Close Date field is blank then a confirmation Dialog should appear asking the user to update the Est Close Date as this field cannot be left blank. If the User select

then apply the

to the field so that user can update the Est Close Date. Else if the user selects

then update the field by default to the current date and pop up the Close Opportunity window to continue. To restore the OOB functionality we would call the same Function Name with the defined patameter we noted earlier. :grinning:

My code as follows:

function OppCloseDate() {

var CloseDate=“estimatedclosedate”);

if (CloseDate.getValue() == null)


Xrm.Utility.confirmDialog(“Est. Close Date of Opportunity cannot be Blank. Update the Close date?”,

function() {

Xrm.Page.ui.controls.get(“estimatedclosedate”).setFocus(); //Set the focus on Est Close Date Field

} ,

function() {

var TodaysDate= new Date(); // Get Today’s current date

Xrm.Page.getAttribute(“estimatedclosedate”).setValue(TodaysDate); // Update the Close Date to Today’s Date

Mscrm.OpportunityCommandActions.opportunityClose(true); // Call the OOB method, Mark Opp as Won




else {

Mscrm.OpportunityCommandActions.opportunityClose(true); // Call the OOB method, Mark Opp as Won



After Publishing the changes to the webresource, will test my jScript functionality. (M having goosebumps, hopefully would run without errors! ) Reopen the Opportunity record and click on the copied button for the new script to run.

Since the Est Close Date is empty, the confirmation Dialog box will appear. As per the script logic all works well.

If select on the Cancel Button, then notice the date in the form field will be updated and Close Opportunity will pop-up, just following the OOB button behavior.

Once tested the functionality is working all fine. Now we can proceed to hide the OOB button. Right click on the button and select the option to hide.

Once hidden the red cross mark will appear on the button and the Hide Action section will be updated with the record under Solution Elements.

Update the same steps for the Close as Lost and all good to go!

Hope that would be helpful to someone. Thanks and have a great day!

责编内容by:Ashwani.Ashwin 【阅读原文】。感谢您的支持!

您可能感兴趣的 partners with Kustomer to provide intellig..., the leading provider of intelligence solutions to high-touch businesses as they scale announces a partnership and inte...
CRM service call returned an error: The top attrib... We might face this issue while using Dynamics CRM Source Component of SSIS Integration Toolkit Warning: An exception has occurred while processin...
Verify that the form was saved or not in CRM 2011 ... I'm writing a Javascript to call external link on click of custom ribbon button in CRM 2011 entity form. In javascript I'm checking the form is dirty...
Magic Clients To Show ROI for xpi Integrations at ... Magic customers show ROI for xpi integrations at SugarCon 2017 Panel -- 2,000 labor hours saved per year, sales productivity increased due to 24/7 rea...
不增长,就出局?教育机构营销如何用「增长思维」赢得竞争?... 随着我国经济的高速发展及人们对学习认知的提升,教育行业迎来井喷式爆发。据有关报道指出,今年前5月,教育行业创投市场合计完成融资案182起,涉及金额152亿元,是去年全年的84%。前几日,VIPKID被爆已完成5亿美元D+轮融资,估值超过200亿元。 然而教育行业中,大者恒大的效应非常明显,这对品牌...