Wednesday, March 23, 2011

CRM 2011 - Populating a required Guid field using JScript

The Problem
Recently I had to use a Guid as a required field of a custom entity (it’s a tracking id). Initially the records were created using an external web service, so I had no issues on populating this field. i.e. when creating a new record, I simply use Guid.NewGuid() C# method to generate a new Guid. However, later there was a new requirement that the users should be able to create new records using the CRM web interface. At that time we had a small issue as the users had to enter a Guid in order to successfully create a record on CRM.

Well there are few tools out there to generate Guids, but for regular users it's not going to be the happiest thing in the world to generate a Guid and enter that on the CRM web form!

The Solution

While there can be number of ways to handle this, I thought if we can generate a Guid via jscript and pre populate the field for the user seems simple enough and neat. In fact, with the introduction of Web Resources feature in CRM 2011, doing this kind of work became really easy.

So the solution has the following components;

1-    Jscript function to generate a Guid

2-    Jscript function to be hooked on to the form OnLoad event

3-    Define a CRM web resource including the Jscript functions

4-    Customize the form to trigger the relevant Jscript functions during the load.

Let’s briefly go through each of these items.

Generate Guid via Jscript

Theoretically a true Guid is based on many things such as timestamps, MAC addresses of network interfaces etc… Unfortunately this kind of functionality does not get exposed by browsers and hence not directly available for us. Therefore, in this case we will use random numbers to make the implementation simple. To fulfill this, I found a really simple implantation on StackOverflow which is based on some work by Shahram Javey.
function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());

Note: In a future post I will explore how we can make a service call to the server to get a true Guid.
Fill the form field for new records

Here we will simply check whether we have some value already filled and if not, use the above function to generate a value and fill it. In CRM 2011, the following function will achieve that. Note that ecm_trackingid is the name of the attribute we need to fill.
function fillGuid() {
var trackingAttr ='ecm_trackingid');
var trackingId = trackingAttr .getValue();
if(trackingId == null){
trackingId = guidGenerator();

Include the Jscript code as a CRM web resource

In order to access the client scripts within CRM, we need to define them as Web resources. So I created a new web resource in my solution as follows.

Although we can define the functions inside a separate *.js file and upload here, I just used the built-in Text Editor to include the functions inline.

Publish all customizations.
Hook the Jscript with Entity Form

The final step of our solution is to hook the Jscript functions to the Form. In this case we want to trigger the fillGuid() method on form OnLoad. To do this will visit the Form Properties of our custom entity. Open the form for edit and click on Form Properties button on the ribbon. First we need to add our web resource under Form Libraries section. Click on the Add button and select our web resource.

Now on the bottom part of the Form properties add an event handler. In this case that is our fillGUID() method.

That’s it! Publish all customizations and go and create a new record. You will see the Guid field is pre-populated!

No comments:

Post a Comment