0
votes

How to transform MS Dynamics CRM ID "on UI" to the ID that API understands (GUID)? Or - How to fetch given CRM entity via API with CRM UI ID?

Example of the ID on UI:

enter image description here

Example of the ID that API understands (GUID):

53d9f073-9418-4d59-9c7f-2f2b43e86051

So I think there is some conversion going on, albeit I have no clue which one? I also know that there are "sequential GUIDs" (to increase performance) but this does not helps much in this "transformation" problem.

There can be of course the other option, that CRM UI hides the real entity ID, but this would be still strange as I can not find the CRM UI ID in the API entity ...

So, I would like to fetch entity by CRM UI ID via CRM API. How can I do it?

2

2 Answers

2
votes

Every entity record in Dynamics CRM has a Guid assigned to it. The field (aka attribute) that holds the guid is EntityLogicalNameid. For example, account is accountid and a custom entity with named new_customentity would have a field called new_customentityid.

During the create process of a new record Dynamics CRM will generate a sequential id for the record. Note: You can specify the id for calls made through the SDK but this is not recommended as it will be a break in the sequence.

This value is not displayed directly on the UI. It can be retrieved via JavaScript by calling Xrm.Page.data.entity.getId().

So knowing that if we want to get the Id of a record you can save the following snippet to a JavaScript WebResource file in Dynamics CRM and register the event DisplayEntityId as an OnLoad function. When the entity is opened it will display an alert box with the entity record's unique id. This of course is just for a proof-of-concept as it would cause serious usability concerns.

function DisplayEntityId() {
     alert(Xrm.Page.data.entity.getId());
}

There is another method to use JavaScript through the browser URL. Open the desired record in Dynamics CRM (and it must be an actual record - not a view, dashboard, etc.) and paste the following into the browser URL. Press Enter and viola the entity record's unique ID will be available for copying.

javascript:var guid=frames[0].Xrm.Page.data.entity.getId();var str1=guid.replace(/{/g,""); var copy=str1.replace(/}/g,"");(function( {window.prompt("Copy to clipboard: Ctrl+C, Enter", copy);})();

Be sure to scroll right to get the entire snippet.

1
votes

What Nick wrote is right. In addition, assuming that your custom Account ID is unique, you can query CRM using a RetrieveMultiple (also a QueryByAttribute) and check if there is only a record returned

string accountValue = "FMA-36056-YGPGY";
ConditionExpression condition = new ConditionExpression("new_accountid", ConditionOperator.Equal, accountValue);
FilterExpression filter = new FilterExpression(LogicalOperator.And);
filter.Conditions.Add(condition);
QueryExpression query = new QueryExpression
{
    EntityName = "account",
    ColumnSet = new ColumnSet(true),
    Criteria = filter
};

EntityCollection accounts = service.RetrieveMultiple(query);
if (accounts.Entities.Count == 0) { /* no records found */ }
if (accounts.Entities.Count == 1) { /* 1 record found */ }
if (accounts.Entities.Count > 1) { /* multiple records found */ }