USD – IsUSD (Alternative Approach)

I love it when I’m asked fantastic questions! Recently someone reported a problem whilst using the window.IsUSD command. If you don’t know this is a JavaScript command we can use to see if our code is being run within USD or not.

The problem was that the window.IsUSD command seemed to no longer work. Probably because they were now using the new Unified Interface rather than the web client or possibly because Chrome Proecss was now being used for the hosting type. One possible (partial) solution is to alter your JavaScript to use “parent.window.IsUSD”. Certainly in some circumstances this appears to resolve the issue.

However even this could still result in an occasional reliability issue! If you are using chrome process as the hosting type then you may still see the IsUSD command fail occasionally. Microsoft explain here. My limited understanding is that the potential “reliability” issues with window.IsUSD relates to the asynchronous nature of chrome process. Meaning, your JavaScript code using a window.IsUSD command could be triggered before the IsUSD value has been set to true. Meaning the code could be unpredictable. One solution is to ensure all of your code is triggered only from predefined USD events such as PageReady. That is fine but you may prefer to add code directly to your Dynamics 365 form. In this post I will describe an alternative approach which you might want to consider.

This alternative approach relies in using a RunScript action when the hosted control is first loaded. By running localStorage.setItem(“isUSD”, “true”) I can then use commands like localStorage.getItem(‘isUSD’) to confirm if the code is being run within Unified Service Desk or not.

I originally blogged about the window.IsUSD feature here. I also showed one possible use for this feature in triggering the load of an account’s website in this post.

Whenever I describe a feature I like to offer an example solution, as in my opinion that helps to give context. Therefore I returned to my earlier example of wanting to load the account’s website from the on-change event on the account form. I decided to update this example, to work with this alternative approach. (And as a result support both the Unified Interface and Chrome Process.)

The steps involved are;

  1. Create a hosted control
  2. Create JavaScript on OnChange of Website
  3. Create a RunScript Action
  4. Create an action to load the account website
  5. Create an event

Tip: This steps might sound complicated but this is a pretty simple example!

Step One – Create a Hosted Control

My first step was to create a hosted control to hold my accounts website. As when a user changes the website field on my account form I want a tab to open containing the newly entered website.

Field Value
Name Account Website
Display Name Website
Component Type Standard Web Application
Hosting Type Chrome Process
Application is Global NOT SELECTED!
Display Group MainPanel

Step Two – Create JavaScript on OnChange of Website

I am going to assume here that you know how to add JavaScript into Dynamics 365 forms!

Essentially what I required was some code that would trigger a USD event each time the website changed. But I didn’t want this code to be triggered if the same onchange event was triggered from outside of USD. You can see below that I have added a form library and in that I have created a function which will be triggered on change of the website field on my account form.

Additionally I have used the edit button and ensured that the “pass execution context as first parameter” option is selected.

The code I added looked like this;

function onChange_Website (executionContext) {
  var context = executionContext.getFormContext(); 
  // *** Ensure website is a fully qualified address
  var website = context.getAttribute("websiteurl").getValue();
  if(website.substring(0, 4) != "http") {
    website = "http://" + website
  }
  var isUSD = localStorage.getItem('isUSD');
  // *** If I am in USD then fire a USD event!
  if(isUSD == "true") {
    window.open("http://event/?eventname=AccountWebsiteOnChange&WEBSITE=" + website);
  }

Notice that I have highlighted in red the command(s) used to check if this code is being run within USD. We will look how to set the “isUSD” value in a second!

Step Three – Create a RunScript Action

Next I want a RunScipt action call that will set my isUSD value.

Field Value
Name Account – RunScript (isUSD)
Hosted Control Account

The tab that holds my account form is called “Account”!

Action RunScript
Data localStorage.setItem(“isUSD”, “true”)

This RunScript action needs to be triggered when my Account hosted control is loaded. So I simply added it to the PageReady event on Account. The end result being that each time my Account hosted control loads a value of “isUSD” will be set to true.

Step Four – Create an action to load the account website

I wanted an action to actually load my account website. That was simply a navigate action.

Field Value
Name Account Website – Navigate (from onchange)
Hosted Control Account Website
Action Navigate
Data url=[[WEBSITE]]

Note: The [[WEBSITE]] parameter is passed in from the JavaScript which runs on the account form. It is just the url of the accounts website.

Step Five – Create an event

Next I wanted to create an event on my Account hosted control, this will be triggered each time the website on the account form changes. I will simply add my navigate action into that event.

Below you can see that I have used the “Related” option to open the “Events” tab. Within the events tab I have simply used the “New Event” button to create an event called “AccountWebsiteOnChange”. This event will be triggered by the JavaScript that will run on my account form.

Once I’d created my event I opened it and added the navigate action we created in step four.

Hopefully within this simple example you can see that I can use a RunScript command including “localStorage.setItem(“isUSD”, “true”)” to set “isUSD” to a true value. Then in any JavaScript I can use “localStorage.getItem(‘isUSD’)” to reference that value. And therefore know if the code is being run within Unified Service Desk or not. Enjoy.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s