Handy little methods for Xrm.Page calls in browser for Dynamics CRM

Based on the last post about retrieving Microsoft Dynamics CRM record based information from the console ‘Dynamics CRM – Access GUID from the browser console‘, I thought I write up some other useful bits that can be used to get information in a pinch.

Most of these have be run from the viewport where the record in question is open, unless otherwise stated.

Get an entity’s origin

frames[1].Xrm.Page.data.entity.getEntityName();

This is particularly handy when you want to know the logical name to work with programmatically. All this without having to open up the solution or form and plow through a few forms and several clicks.

Get logical name for controls in the controls collection

frames[1].Xrm.Page.getControl(0).getName();

The controls collection is a child of the ui object, which itself is a child of the Xrm.Page object. [See the visual layout on MSDN]. The index allows retrieval of control items as they occur on the form.

Get logical name for control based on display name

frames[1].Xrm.Page.getControl(0).setFocus();

Now there are no direct method calls, that I found or aware of, that could get you the logical name for a field based on its display name. Which is a big shame because that would be a very useful one.

Now this might be somewhat tedious based on what wants to be achieved, and most definitely based on the number of fields on the form, as the user will have to randomly call the setFocus() method on a guess of where the control might be in the index based on position i.e. if the field is placed in the middle of the list of fields, chances are the index will be roughly half of the length of the count of fields.

But once the focus reveals the field that the user is interested in, the previous method getName() can be used to retrieve the logical name using that same index.

Conclusion

These calls are part of the Xrm.Page model which can be referred to on the MSDN site. However, they can be utilized for quick little views at relevant information via the browser’s console.

Dynamics CRM – Access GUID from the browser console

Now this blog hasn’t quite morphed from its primordial form and I have yet to declare its mission statement.

In a nutshell it is to log and explore all aspects of my  journey through learning JavaScript – its many utilities and facets. One such area is its use in Microsoft Dynamics CRM.

So without discussing further details of background of JavaScript use in CRM, I’d like to rehash a quick and easy way to retrieve a form’s GUID (Globally Unique Identifier) through the browser console. Just to point out that this tip comes from other sources [1] [2] but I noticed it wasn’t quite working.

The posts suggests entering the following into the browser console:

frames[0].Xrm.Page.data.entity.getId();

However doing so in Firefox throws an error saying that data node is null.

XrmPagedata

The issue is that the first index of frames i.e. frames[0] is actually referring to the list of records from the current view and not the actual record instance itself.

Typing in frames[0] in the console illustrates this:

XrmPagedataframes

This returns _root branch before the page itself. Clicking on this should display the list of records. The implementation seems to have undergone changes since CRM 2013.

In order to access the record directly and therefore its GUID the correct frame index is 1:

XrmPagedataframes1

Clicking on the link returned will open the record in a new page. So with this index change the call mentioned in the original blog(s) will return the record’s GUID.

XrmPagedataentitygetId

Note: I haven’t tried it on IE or Chrome as IE keeps crashing constantly when the developer tools is opened. and I just haven’t bothered with Chrome.