Sometimes, you may have a requirement to get the most recently created on record from the entity collection you have retrieved.
Instead of playing around a lot with coding and .net stuff, Dynamics CRM fetch XML and QueryExpression provides a way to sort records Ascending or descending.
Therefore, While retrieving records in FetchXML, do this :
<attribute name='atrributename1' />
<attribute name='atrributename2' />
<attribute name ='attributename3' />
<order attribute='createdon' descending='true' />
<condition attribute='statecode' operator='eq' value='0' />
Or in Query Expression:
QueryExpression qe = new QueryExpression(entityName);
FilterExpression fe = new FilterExpression();
qe.ColumnSet = new ColumnSet(true);
qe.Orders.Add(new OrderExpression(columnname, ordertype));
when Execute them , you will get the desired record on the top which can be access by simply by doing retrievedResult or :
firstRecord= retrievedResult.Entities.First(); //first method.
I hope this helps!
Sometimes we have a requirement to retrieve audit history changes for a particular field in CRM which may be for the purpose of checking if for e.g field A value which is at the moment set to “10”, was ever “5” or may be “3” or may be use old those values of fields to create a new CRM Record. In the below Image , you can easily retrieve old and new values at any given time on birthday field by looping on each audit record:
I have taken the reference from Dynamics CRM SDK & MSDN to figure out how this can be achieved.
Please note : To be able to use this you must have Auditing enabled on all three areas of CRM.
1. Auditing for whole Organization should be already turned on.
2. Auditing for Entity Should be already turned on.
3. Auditing for that Field should already be turned on.
I have accomplished this on a console application , you can do it in plugins or custom workflows as per the requirement.
Assembly: Microsoft.Crm.Sdk.Proxy (in Microsoft.Crm.Sdk.Proxy.dll)
//initiate a new retrieve request & add entity logical name + Guid of the record to retrieve
RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
changeRequest.Target = new EntityReference("contact", Id); RetrieveRecordChangeHistoryResponse changeResponse =
//Execute the request, the "details" variable will have the audit data.
AuditDetailCollection details = changeResponse.AuditDetailCollection;
// Retrieve Particular attribute change history by passing entity logical
//name and guid of the record, finally execute using RetrieveRecordChangeHistoryResponse
var attributeChangeHistoryRequest = new RetrieveAttributeChangeHistoryRequest
Target = new EntityReference("contact", Id),
AttributeLogicalName = "birthday"
var attributeChangeHistoryResponse = (RetrieveAttributeChangeHistoryResponse)_service.Execute(attributeChangeHistoryRequest);
details = attributeChangeHistoryResponse.AuditDetailCollection;
//Details will have many records for example birthday change records, loop through all of them
foreach (var detail in details.AuditDetails)
var detailType = detail.GetType();
if (detailType == typeof(AttributeAuditDetail))
// retrieve old & new value of each action of each audit change from AttributeAuditDetail
var attributeDetail = (AttributeAuditDetail)detail;
foreach (KeyValuePair<string, object> attribute in attributeDetail.NewValue.Attributes)
string oldValue = "(no value)", newValue = "(no value)";
oldValue = attributeDetail.OldValue[attribute.Key].ToString();
newValue = attributeDetail.NewValue[attribute.Key].ToString();
if (oldValue !=null & newValue!=null)
// Do Something here!
catch (Exception ex)
throw new Exception(ex.Message);
I hope this helps!
Concept of Alternate Keys has been introduced with Microsoft Dynamics CRM Version 2016.
All Microsoft Dynamics CRM records have unique identifiers defined as GUIDs. These are the primary key for each entity. However if you have alot integrated systems in your organization – it would be very difficult for other systems to retrieve or update records using the guids as they wont necessarily store CRM’s internal GUIDs. Hence you can create an alternate key which is actually a data in other systems – they can easily update or retrieve data from CRM using this.
Only below field types can be added as an alternate key :
- Decimal Number
- Whole Number
- Single Line of Text
- Date Time [ Only with Dynamics 365 v9 or above]
- Lookup [ Only with Dynamics 365 v9 or above]
- Picklist [ Only with Dynamics 365 v9 or above]
Point to note is that for a single entity, only 5 alternate keys are allowed in CRM.
Apart from Above , Alternate keys will stop creating duplicate records in the system, be it normal creation , excel import , or from external service.Wow how cool is that!
To Create an alternate key , go to customization > Entity> Expand the entity and look for “Alternate Keys” option > click on New and give display name and schema name > select the field for which you want to enable it > click on the right arrow > click on Save.
when you save an alternate key it, you are actually creating an async job which goes through following status :
- In Progress
Once status shows “Active” it means its ready to be used:
you can very well use alternate keys with WebApi , take reference from the below :
Write to me if you have any questions on this topic.
I hope this helps!
As per the recent communication from Microsoft, Microsoft Dynamics CRM SDK will not be available on Microsoft downloads. If you are working on Dynamics 365 v9, well you have to download every component which was available in the SDK individually.
Every tool will be available via NuGet Manager.
The name will be : Developer Guide not Dynamics CRM SDK.
For more information, visit the below blog : https://blogs.msdn.microsoft.com/crm/2017/11/01/whats-new-for-customer-engagement-developer-documentation-in-version-9-0
Here is the step by step information on how to download these tools using powershell : https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/download-tools-nuget
i hope this helps.