How to Prevent Ribbon Actions Based on User Input (CRM 2011)

Tags: ,

To kick things off, here’s a classic blog from a few years back.

Alright ladies and gents, this is a fairly hefty blog post but is also very informative.

I was once asked to implement a requirement where orders could not be submitted if one or more Quote Products was a write-in in Microsoft Dynamics CRM 2011.

To do this, we needed to modify the button in the Ribbon that submits the order. Here’s a simple way to do that. It looks complicated but it’s really not that bad!

Here’s a step-by-step guide:

Using the Visual Ribbon Editor on Codeplex (http://crmvisualribbonedit.codeplex.com/) , open your Organization and the Quote entity.

Notice that the Create Order button is there, but since it’s a System button, it can’t be edited. So we’re going to have to replace it with our own. Make a new button and copy the Display Rules from the native button. In this case, the “Entity Salesorder Privilege = Create Basic” rule is an EntityPrivilegeRule. Under Enable Rules, copy the JavaScript path and function names into two new rules. Hold off on the Action section for now.

Your new button’s configuration will look like this so far:

1

2

3

Now comes the harder part. You need to make your own jScript function that will run a quick web service call to see if there are any Quote Products that are write-ins.

First, make an Advanced Find with the query you want. In this case, we are looking for Quote Products where the Quote is the specific one we are on. So when making the Advanced Find, pick any Quote in the system for now. Later we’ll have the jScript swap out the individual Quote GUID on the fly.

Here is our Advanced Find:
4

Download the FetchXML into a Text file. This is going to be added to jScript so we need to wrap each line in quotes to turn it into a string (meaning the single tick at the beginning and the single tick at the end, the plus concatenating it):

'<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'+
' <entity name="quotedetail">'+
' <attribute name="productid" />'+
' <attribute name="productdescription" />'+
' <attribute name="priceperunit" />'+
' <attribute name="quantity" />'+
'<attribute name="extendedamount" />'+
' <attribute name="quotedetailid" />'+
' <order attribute="productid" descending="false" />'+
' <filter type="and">'+
' <condition attribute="isproductoverridden" operator="eq" value="1" />'+
' </filter>'+
' <link-entity name="quote" from="quoteid" to="quoteid" alias="ad">'+
' <filter type="and">'+
' <condition attribute="quoteid" operator="eq" uiname="Interested in our newer offerings (sample)" uitype="quote" value="{6EE28A24-602A-E111-8EBC-1CC1DEE87ACD}" />'+
' </filter>'+
' </link-entity>'+
' </entity>'+
'</fetch>';

Now we are going to make a jScript function out of this that will tell us the quantity of Quote Products that match. This requires the XRMServiceToolkit from Codeplex (http://xrmservicetoolkit.codeplex.com/). You don’t have to know how it works. Download the code from Codeplex and add it to your system as a Web Resource of type Script. Then add the Web Resource to your form (Form Properties, then Form Libraries). Back in your text editor, make a jScript function out of your query.

function getQuantityWriteInOrders()

{

var fetchQuery =
'<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'+
' <entity name="quotedetail">'+
' <attribute name="productid" />'+
' <attribute name="productdescription" />'+
' <attribute name="priceperunit" />'+
' <attribute name="quantity" />'+
' <attribute name="extendedamount" />'+
' <attribute name="quotedetailid" />'+
' <order attribute="productid" descending="false" />'+
' <filter type="and">'+
' <condition attribute="isproductoverridden" operator="eq" value="1" />'+
' </filter>'+
' <link-entity name="quote" from="quoteid" to="quoteid" alias="ad">'+
' <filter type="and">'+
' <condition attribute="quoteid" operator="eq" uiname="Interested in our newer offerings (sample)" uitype="quote" value="' + Xrm.Page.data.entity.getId() + '" />'+
' </filter>'+
' </link-entity>'+
' </entity>'+
'</fetch>';
var returnArray = XrmServiceToolkit.Soap.Fetch(fetchQuery);
if (returnArray == null)
{ return null; }
return +returnArray.length;
}


A couple of things here. Note that in the “condition attribute” clause, I replaced what was the GUID from the Fetch download with the Xrm.Page.data.entity.getId() method. This will insert the GUID of the current Quote you are on at run-time. In short, it makes the query say, get me a list of Quote Products that are write-ins that are associated to the Quote I am looking at right now.

XRMServiceToolkit will return the results of the query as an array. Since we are only interested in the quantity, we can just return the length of the array. The + operator forces the return to an integer value.

Now we create the control function. This will simply run the query, get how many Quote Products match, and if the number is greater than zero, die.

function checkOrders()
{
if (getQuantityWriteInOrders() == 0 )
{
acceptQuoteOrCreateOrder();
}
else
{
alert ("You can not submit orders with write-in products.");
return null;
}
}

Where did acceptQuoteOrCreateOrder() come from? It’s the native function within CRM that pops the Quote Submit screen. We know this from the definition of the original Create Order button back in the Ribbon editor:

5

Basically, the checkOrders function runs the query and only lets you submit the order if the quantity of Write-In products is zero.

Take the two functions and add them to CRM as a Web Resource, then add it to the Form. Your form Properties will look like this:

6

No need to set triggers onLoad, onSave or onChange since we are going to fire this code when the user clicks the Ribbon button.

Now, we need to add the Action to our new custom button. Back in the Ribbon editor, add a JavaScript function under the Action tab.

5

checkOrders is the name of the function we wrote above. To get the Library URL, it’s at the bottom of the Web Resource configuration screen:

ribbon8

In this case, we pull off the beginning part and keep everything after WebResources; start with one slash not two (“/WebResources/new…”)
Lastly, use the toggles in the Sequence field to bump the button left or right to be next to the old one.

Once you save your Ribbon in the Editor, your new button will be published.

Once it works, you change the Label of the new one to “Create Order” and disable the original button by clicking Hide. Even though you’ve replaced the button, it looks like the original button to users.

Leave a Reply

Your email address will not be published. Required fields are marked *