CRM 2013 – Javascript to get id of current record

It’s a quick tip for you today, it took me longer to test it and then to write the javascript. The problem I had was when I pressed F12 to debug the Javascript on CRM 2013, I could see the name of my javascript file and in the end I had to search for some of the Javascript, which brought up a temporary file where I could put my breakpoint in.

var id =Xrm.Page.data.entity.getId();

var entityName = Xrm.Page.data.entity.getEntityName();

It’s worth getting to know the other functions in Xrm.Page.data.entity

The useful ones are above but there is also getIsDirty, which can be used to see if a field has been modified because only modified fields are sent to the server to be saved.  Although that is my CRM 2011 thinking because in the CRM 2013 world fields are sent back to the server every 30 seconds to be saved (unless you have turned autosave off)

Xrm.Page.data.entity

Method Description
addOnSave Sets a function to be called when the record is saved.
getDataXml Returns a string representing the xml that will be sent to the server when the record is saved.
getEntityName Returns a string representing the logical name of the entity for the record.
getId Returns a string representing the GUID id value for the record.
getIsDirty Returns a Boolean value that indicates if any fields in the form have been modified.
removeOnSave Removes a function from the OnSave event hander.
save Saves the record. This method has three possible parameters.

3 thoughts on “CRM 2013 – Javascript to get id of current record

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