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.



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. :grinning:

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.

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 (源链)。感谢您的支持!

您可能感兴趣的

CRMNEXT, Inc. Partners with OneSpan to Improve the... SAN FRANCISCO–(BUSINESS WIRE)– CRMNEXT, , the leading global CRM sol...
Transform Customer Experiences by Harnessing the P... View from the Marketplace Views from the Marketplace are p...
Friday rant – UK high speed internet provisioning ... Returning to the UK after a very long absence has been an eye opener on a n...
智能名片:资本风口上飘扬的那张小纸片... 资本对IT圈的口味偏好总是那么让人捉摸不定,早先的CRM,再然后的社交类;去年的区块链,今年的AI,但此时此刻,站在风口上的创业者们手中挥舞的却是一张小小的名片...
How to Use Cloud Elements Authentication APIs The first milestone in building an integration is authorizing your endpoints to...