USD – Execute Actions from JavaScript

In a previous post, I explained how we could trigger events from JavaScript when using Unified Service Desk (USD) for Microsoft Dynamics 365. In this post, I will show how a similar approach can be applied to actions in USD.

If you’d would like to view that previous post on events, you can find it here.

I always like to explain things by showing an example. In this case my example will be a small feature I have implemented in my USD application.

When viewing an account (or other entity) I can add contacts from a sub grid on the account form. Doing this will open the contact Quick Create form. I wanted to have a note on the account that a contact had been created. Meaning I’d need to trigger a USD on the save of the contact.

I could have achieved this requirement in several different ways! But I decided to use an action called from JavaScript.

This simple example was very easy to achieve!

You can see the end result of my change below. I have added a contact and a note has been created on the account to say who created the contact and when.

I simply created a small amount of JavaScript that was added to the “On Save” event of my CRM Quick Create form. My code looked like this;

function OnSave() {

    // ** When we save a contact, in quick create form. 
    // **    then ...
    // **        if in USD then fire an action that will add a note to the account (or other entity)
    // **        the note will show us that a contact has been added.
    // ** Notice that I use %0D%0A, this gives an essential line feed!

    var name = Xrm.Page.getAttribute("firstname").getValue() + " " + Xrm.Page.getAttribute("lastname").getValue();
    if (window.IsUSD == true) {
       var data = "LogicalName=annotation " + "%0D%0A";
        data += "objectid=EntityReference(\"[[$Context.LogicalName]]\",[[$Context.Id]]) " + "%0D%0A";
        data += "notetext=$Escaped(CONTACT ADDED Name is " + name + ")";"http://uii/CRM Global Manager/CreateEntity?" + data);
    } // End if
} // End function


Hopefully you can see in my window.IsUSD command how I ensure this code only runs whilst inside USD. Also notice the command that contains the instruction to run the USD action. This is essentially made up of the hosted control name, action name and data parameters.

Tip: My data parameter has several lines, this means I need to add a carriage return / line feed at the end of each line. I do this with %0D%0A!

In my case the hosted control was my global manager and the action was CreateEntity. You can find details of how we can use the CreateEntity action to create notes here.

Hopefully you can see that it is actually pretty easy to adapt this logic to work with any action in USD. J

2 thoughts on “USD – Execute Actions from JavaScript

  1. Pingback: USD – The Book | Microsoft Dynamics CRM and Unified Service Desk

  2. Pingback: JavaScript – My Collection | Microsoft Dynamics CRM and Unified Service Desk

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s