Easy Access to the crmForm.ObjectId from CRM 4.0 entities

Note: The content in this post, like most of my posts, is likely unsupported.

Warning: This is a lot of content for a very few lines of code. I ramble so that you can learn the how and the why. More advanced developers and/or less patient readers may want to go straight for the code.

If you’re anything like me, you spend a lot of time in CRM. I love it, as does most anyone I’ve met who’s tried it. Having said that, there are a few things about it that are intentionally hidden from the user’s view and with good reason.

A great example of that is the address bar. For most of us who deal with CRM, we know that CRM keeps track of entities by using GUIDs. Furthermore, we know that the easiest built-in way to get the current entity’s GUID is to get it from the URL. One quick way to do that is to hit F11 which will cause the current browser window to go full screen and the address bar is available by moving your mouse to the top of the window. It’s a little annoying because it usually takes me 3 steps:

  1. You have to hit F11 and move the mouse to the top of the window to show the address bar.
  2. You have to click in the address box, which usually makes the address bar hide itself, so you have to click in it again.
  3. You have to highlight only the GUID before copying it, or copy and paste the whole URL and remove everything but the GUID.

Now if you haven’t been exposed to that method, you’re welcome, but hang on because this post should save you the trouble. 3 repetitive steps that annoy me and are preventable are usually enough for me to search for a programmatic solution. I’m a developer and I live by DRY. (Don’t repeat yourself)

So there are many ways to accomplish this I’m sure, but here’s my way. I should mention here that I tend to deal with the database side of things a great deal as well, so I knew from the beginning that I wanted to copy the GUID to my clipboard.

Initially my thought was to add a button to the toolbar, or maybe even add a copy command to the actions menu, but while I was making this little snippet I realized that often times I’m running database queries against these GUIDs and it would be nice to see the GUID on the screen at all times. I decided to tap into the status bar. For all the entities I checked it’s always down there, and it seems to always have room enough for some other text, say… a GUID. 🙂

My approach: This is actually pretty straight-forward. Each entity’s form has a crmForm.ObjectId (yes, case sensitive) that is the GUID for the current record. So, I figured I would add some JavaScript to embed the ObjectId in a UI element on the form. Nowadays, anytime I hear JavaScript I think jQuery. So, how to get jQuery on a CRM form? This has been documented in several places on the interwebs. Before I post it here online, I need to ask the person I got it from if I can share this particular approach.

Now that you’re referencing jQuery, you have a little horsepower behind you. So, let’s add the line that will show the GUID in the status bar:

$("#crmRenderStatus").closest("td").append("<span id='GetObjectId'>" + crmForm.ObjectId + "</span>");

Huh? What’s all that? Let’s break it down:

  • First, it finds the element whose id is crmRenderStatus.
  • Next, it finds the closest td tag (in this case, its parent td)
  • Then, it appends the HTML that I’m specifying while injecting the crmForm.ObjectId

Here’s the result:


What you wind up with is quite simply the GUID nested in a span tag, positioned right next to the Status on the status bar. It’s important to note that the fact that I’ve tucked the content in a span tag with a specific ID property is both intentional and required. Great! Now I can see the GUID, but I can’t copy it. How can I fix that? Easy enough. Just use good old jQuery again and wire up a double click event handler to the GetObjectId span that contains the GUID, like this:

$("#GetObjectId").dblclick(function() {
    window.clipboardData.setData('Text', this.innerHTML);

Does that look like Greek? OK, let’s break that down:

  • First, we’ll find our newly added span element with the id of GetObjectId.
  • Next, we’ll attach a handler to the double click event (jQuery .dblclick method).
  • Finally, we’re defining an anonymous function that details what we want to do when the double click event fires.

Now this works, but you don’t get any visual indication that you’ve copied the GUID. To handle this, I’m changing the “UI modification” line to include a span that just has the text ” Copied…”. This line starts out invisible since you haven’t copied the GUID until you double click on it.

$("#crmRenderStatus").closest("td").append("<span id='GetObjectId'>" + crmForm.ObjectId + "</span><span id='ObjectIdCopied' style='display:none;'>&nbsp;Copied...</span>");

I’m also going to change the handler’s anonymous function to show the ” Copied…” text by fading it in and to allow them to get a second copy indication by fading it out.

$("#GetObjectId").dblclick(function() {
    window.clipboardData.setData('Text', this.innerHTML);

This results in a brief moment that looks like this (insert imagined fade-in and fade-out here)

There we have it. A few lines of code that save me some hassle many times throughout the day. You can see the detail in this post by Jim Wang, but the other thing that I do is wrap this in an if statement that checks to make sure that all this only happens if you’re a System Administrator or System Customizer. Alright, I warned you this was a long post for such a small amount of code. Here’s the code all wrapped up. Hope this helps!

if (UserHasRole("System Administrator") || UserHasRole("System Customizer"))
     $("#crmRenderStatus").closest("td").append("<span id='GetObjectId'>" + crmForm.ObjectId + "</span><span id='ObjectIdCopied' style='display:none;'>&nbsp;Copied...</span>");
         window.clipboardData.setData('Text', this.innerHTML);

There is one other addition to all of this which you may have noticed. We use a UserHasRole method to make sure that I’m not showing everyone the GUID. That’s not because I want to keep it a secret, it’s because I don’t need users telling me that CRM is emiting corrupted text.


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