, , , , , , ,

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.RibbonWorkbench3-2


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


Create a new connection to your Organization. RibbonWorkbench2-2


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



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


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. RibbonWorkbench6-2


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


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. RibbonWorkbench8-2


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


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. RibbonWorkbench10-2


Likewise, paste the same under the command section.RibbonWorkbench11-2


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.RibbonWorkbench12-2


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


Also update the Command Core properties. RibbonWorkbench14-2


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. RibbonWorkbench15-2


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.RibbonWorkbench16-2


Now click on Add Actions and select the option Javascript Action.RibbonWorkbench17-2


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.RibbonWorkbench18-2


Once Done, Publish the changes. RibbonWorkbench19-2


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! 😀 RibbonWorkbench21-2



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. RibbonWorkbench22-2


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.

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 OK then apply the focus to the field so that user can update the Est Close Date. Else if the user selects Cancel 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. 😀

My code as follows:

function OppCloseDate() {

var CloseDate= Xrm.Page.data.entity.attributes.get(“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.RibbonWorkbench25-2


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. RibbonWorkbench26-2


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.RibbonWorkbench27-2


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.RibbonWorkbench28-2

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! 🙂