Asynchronous CRUD operations in USD using CRM V9 updates.

It’s been quite a long time by posting a new blog post, bit occupied with our Jr. all these days 🙂 ..

I would like to start with the almost new version of USD 3.0 in my come back blog. There are quite a good amount of features present in the new version and 1 particular new feature I was excited about when it is announced is CreateEntity in an Asynchronous way. I don’t want to blog it again as the USD guru @Neil already documented here.

Though I was excited so much when I first read about this feature but it didn’t take much time for me to realize that #INeedMore

a) It’s only CreateEntity is async. In a tweet, @Sid is kind enough to confirm that this is a last moment addition and we can expect other CRUD ops as well in the async mode in next versions. (Excited again to see the new features in to play 🙂 )

b) No way to identify the exceptions – By any reason if the CreateEntity(async) failed, There is no way that we will come to know about it. Trust me, this going to be a big problem in real production env.

c) Have to wait for the result (using special actions like WaitFoData) if the result is required to process further actions. Somehow, I’m not so comfortable with such kind of waiting conditions.

d) In CreateEntity, It always confuses me and even took a long time to identify on how to pass values for special fields like Money, decimal/double etc.

So, I was on a hunt for a better solution and CRM V9 (the so-called July Release), gave me some pretty good points to solve these problems by using the new Clientside scripting namespace Xrm.WebApi.

Let me quickly jump into action and show you all how can we leverage this new Xrm.WebApi in conjunction with USD and solve the above-mentioned problems.

Step#1: I need a CRM page which is always available for me to use the RunXrmCommand action. The safest I can think of is, SystemUser page which in most cases will not have any additional javascript and I will be anyways loading it in the hiddenpanel, no one knows about it. Here is my hosted control configuration for SystemUser.

Field Name Field Value
Name Some meaningful name, Mine is – Global SystemUser CRMPage (Hidden)
Unified Service Desk Component Type CRM Page
Application is Global True
Display Group HiddenPanel
Application is Dynamic No

Leave all the other configs with default values.

Step#2: Create an action call to load this with logged in user when USD loads.

Field Name Field Value
Name Some meaningful name, Mine is – Navigate to System User record
Hosted Control Control created in the previous step (ie. Global SystemUser CRMPage (Hidden))
Action Navigate
Data url=main.aspx?etn=systemuser&id=[[systemuser.systemuserid]g]&extraqs=&pagetype=entityrecord

NoScan=True

HideRibbon=True

HideNav=True

Notice the usage of [[systemuser.systemuserid]g] replacement parameter in Data. Now that we have the action call, Attach it to DesktopReady event so that the navigation occurs when the USD is ready to use. Here is how my DesktopReady event looks like.

Step#3: Now we have the framework ready, All we have to do is firing up required CRUD actions asynchronously. Let’s take a scenario where we want to create a phone call record asynchronously whenever a session is started and open it after it is created. Let’s go ahead and create a new Action call which fires Xrm.WebApi command.

Field Name Field Value
Name Some meaningful name, Mine is – Create Phone call asynchronously
Hosted Control Control created in step#1 (ie. Global SystemUser CRMPage (Hidden))
Action Navigate
Data

var data ={

"subject": "New Incoming call",

"directioncode":false

}

Xrm.WebApi.createRecord("phonecall", data).then(

function success(result) {

window.open("http://event/?eventname=PhoneCallCreataed&id="+result.id);

},

function (error) {

window.open("http://event/?eventname=PhoneCallCreationFailed&error="+error.message);

}

);

No need to be panic with a big “data” field. If we evaluate line by line, All we are doing is creating the data JSON object and passing it to Xrm.WebApi.crateRecord method. On Success we are firing PhoneCallCreated event with ID as parameter and on Failure we are firing PhoneCallCreationFailed event with error message as parameter.

Attach the action call to NewSession event and create the PhoneCallCreated and PhoneCallCreationFailed events on your SystemUser hosted control.

That’s it – you are all set to restart your USD and test. The phone call will get created asynchronously and eventually fires the PhoneCallCreated event where you can simply use open_Crm_page to open the phone call entity if you want. This is how my new phone call got created and opened when I start a new session.

I can see a lot of potential advantages to this approach apart from covering the above-mentioned issues for ex: creating parent and child records at a time, calling an action, executing as an Admin user etc.

Hope it helps you all, Bye for now 🙂

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 )

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