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!
While importing a solution I got a generic SQL error but when i downloaded the log file i saw below error:
0x80044150 – Object reference not set to an instance of an object
I have seen this error in plugins several times but never in import but root cause and underlying issue remains the same.
The import was trying to find something which was not there in target.
There was a relationship in my solution exist as N:1 to an entity which was deleted from target system way back hence this error occurred. Using the relationship given in solution, CRM was trying to find related entity which wasn’t there.
Hope this helps.
Whenever you open Dynamics CRM , you are presented with a default screen; which you can turn off. However if you have a requirement to put something when users login on the welcome screen i.e Guidelines, Important Links etc., You have multiple ways of achieving this, some are supported and others are unsupported/Not Recommended.
In this blog I am going to quickly share a very simple & supported way of managing Microsoft Dynamics CRM Welcome Screen. I’m using a dashboard to show my desired welcome screen using an HTML web resource. Here is how it looks:
Lets discuss this step by step:
- Disable the out of the box welcome screen, to do this
Goto Click on Main -> Settings -> Administration.Under General Tab, Find Set whether users see welcome screen option. Click No and Ok.
- Create a System Dashboard with just one section in it. If you cant find that one since CRM give you options with mutiple sections. what you can do it “Save as” an existing dashboard , in my case used ” Social Engagement dashboard”
- This dashboard has just one big section which , there’s web resource being used. All we have to do is change its name and also use a new web resource.
- Create a web resource in CRM and select a type as HTML. To Generate a quick HTML i have used this website : https://html-online.com/editor/ Its very nice and easy.
Save your web resource and Publish it.
- Final step is to go to the dashboard > Double click on it and select the webresource section and click on “Edit Component”:
- Now replace webresource section with your HTML web resource. Click Okay and publish all customization.
- Final Step is make this dashboard default for all user. To do this; select the dashboard and click on “Set as Default”:
- Save and publish all customization and you are done. Whenever you or any user open dynamics CRM, will be presented with our custom screen like below:
I hope this helps! Obviously its a solid solution, however in terms of development efforts and Supported item for dynamics CRM, this is one of the best solution.