Stop Assign functionality based on Business Logic using Client API in Dynamics 365

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

Assign – an age old functionality which is there since the time I started working in CRM which dates way back to 2011. So much we have seen about Assign that whenever I talk about Assign it’s like going back to our school days. But sometimes stuff from our school days we miss out which can be quite interesting when we discover it in our later years.

So here was this requirement. When a user clicks on the Assign and try to assign a record to a user or team, there should be some custom business validations based on which we will decide whether to go ahead with the Assign. If not then we will throw a custom message to the user.

So let’s see the tried and tested options and see why they won’t fit here.

Writing a plugin

For me it’s the best solution because it handles server side scenarios as well. However while the plugin shall still be there, the error dialog throw from plugins is not acceptable by the end-users here. They want more of like an alert dialog available in Dynamics 365.

Show hide the Assign button using enable rules

While this works, it is absolutely essential for this validation to be as real time as possible. Also the customer wanted that the end-user should understand why he/ she is not able to assign the record and then take corrective action.

So what’s the other way? We all know that when Assign operation is performed, the record is saved and any save event you have registered fires as well. And here is secret. There is a specific save code for Assign which is 47.

Using the specific save code we can understand whether Assign is currently being executed by the user and take necessary action.

function record_onsave(e) {
    var saveMode = e.getEventArgs().getSaveMode();

   if (saveMode === 47) { // assign
       // perform your custom logic here
       // and show alert using Xrm.Navigation.openAlertDialog if assign is invalid.

      // also you can stop the save using code below.
       e.getEventArgs().preventDefault();
    }
}

Using this simple trick you can actually handle your assign event. One very important point to note here. If you expose the owner field on the form and the end-user updates the owner field, that is not considered as Assign operation and would be treated as regular save (savemode = 1). So your above code shall fail.

Hope this helps!

Debajit Dutta

(Microsoft MVP)