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 【阅读原文】。感谢您的支持!


Salesforce三项新服务让CRM具备认知能力 Salesforce近期在博客中公布了其爱因斯坦AI平台上的三项认知服务。这三项服附加务包括情感检测、意图检测以及目标检测。Salesforce的用户可以通过这三项服务来自动完成洞察分析,还可以在他们的CRM应用中使用预测模型。Salesf...
The Third Transformation of CRMs — CRM B2B sales, ... The Third Transformation of CRMs — CRM B2B sales, Intelligence, Productivity For anyone who works in sales or mark...
Salesforce ups its AI game with a customer service... Salesforce cofounders Marc Benioff and Parker Harris dance with an animated version of Einstein, the m...
iDonate Secures $10M Series A Funding Capital infusion accelerates growth in enterprise donor engagement business through new personalized connections, anal...
云之家CRM链接ERP项目总结 踏入云之家CRM数月,通过数月的了解和实战,写一下自己在CRM链接ERP项目中的学习和总结。 关于云之家CRM 云之家CRM(全称客户关系管理customer relationship management)是一款面向...