Setting a CRM 4.0 Lookup field in JavaScript – part deux

Yes, some time has passed since I last posted a blog article and yes, I could say that it won’t happen again but I don’t want to make any promises. Since my last post a lot of things have changed in my life, so my apologies. That said, we aren’t here to discuss me. We’re all about the CRM. If I can keep my focus I would expect many new blogs posts about CRM 2011 as well as some CRM 4.0 blog posts that I’ve been meaning to get out the door. For this post however I just wanted to drop a little something for everyone, so here goes.

You may recall that I had previously posted and explained how to set the value of a Lookup field in CRM 4.0. Well, I’m going to quickly show how to take that to the next level to keep your life (actually, your code) clean and concise.

First, let me post a refresher of the SDK code to set the value of a Lookup field:

var lookupObject = new Object;
var  lookupObjectArray = new Array();
lookupObject.id =  "21EC2020-3AEA-1069-A2DD-08002B30309D";
lookupObject.name = "John Doe";
lookupObject.typename = "systemuser";
lookupObjectArray[0] =  lookupObject;
crmForm.all.new_userid.DataValue = lookupObjectArray;
crmForm.all.new_userid.ForceSubmit = true;

Now that code comes from the SDK, and is intended to allow people to follow along and understand what’s happening. Long story short, there’s another notation called JSON (JavaScript Object Notation)

json Read more about JSON by visiting http://JSON.org

I won’t waste your time explaining what the JSON site can explain to you, but we can shorten our code by using JavaScript Object Notation. I’ll post the same code block getting smaller each time so that you can see what I’m doing. First, you don’t have to use the “new Array()” syntax to create an array. In fact, many JavaScript gurus recommend against it. Instead, you can simply use the square brackets, like this:

var lookupObject = new Object;
var  lookupObjectArray = [];
lookupObject.id =  "21EC2020-3AEA-1069-A2DD-08002B30309D";
lookupObject.name = "John Doe";
lookupObject.typename = "systemuser";
lookupObjectArray[0] =  lookupObject;
crmForm.all.new_userid.DataValue = lookupObjectArray;

JavaScript also allows to use the curly braces to declare an object, as shown here:

var lookupObject = {};
var  lookupObjectArray = [];
lookupObject.id =  "21EC2020-3AEA-1069-A2DD-08002B30309D";
lookupObject.name = "John Doe";
lookupObject.typename = "systemuser";
lookupObjectArray[0] =  lookupObject;
crmForm.all.new_userid.DataValue = lookupObjectArray;

Looking at the code like this may make you consider whether we actually need the lookupObjectArray variable at all, and in fact we don’t. We could simply do this:

var lookupObject = {};
lookupObject.id = "21EC2020-3AEA-1069-A2DD-08002B30309D";
lookupObject.name = "John Doe";
lookupObject.typename = "systemuser";
crmForm.all.new_userid.DataValue = [lookupObject];

Now to use JSON, we can declare our object in one statement. All you C# folks may find that it looks a lot like automatic initializers. Here goes:

var lookupObject = {
        id: "21EC2020-3AEA-1069-A2DD-08002B30309D",
        name: "John Doe",
        typename = "systemuser"
    };
crmForm.all.new_userid.DataValue =  [lookupObject];

Finally, if we got this far you may have guessed that we don’t really need lookupObject and we can instead simply declare it inline, like this:

crmForm.all.new_userid.DataValue = [{
        id:  "21EC2020-3AEA-1069-A2DD-08002B30309D",
        name: "John Doe",
        typename =  "systemuser"
    }];

There we have it. Doesn’t that look more clean and concise than what we started with? I think so, and I hope you do too. Cheers!

Advertisements

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 )

Google+ photo

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

Connecting to %s