Tag: Dynamics CRM SDK

Retrieve Audit History Changes For A Particular Field/Attribute Of A Record

Description:

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:

audit1

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.

Solution Code:

I have accomplished this on a console application , you can do it in plugins or custom workflows as per the requirement.

Required :

Namespace:   Microsoft.Crm.Sdk.Messages

Assembly:  Microsoft.Crm.Sdk.Proxy (in Microsoft.Crm.Sdk.Proxy.dll)

try
   {
//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. 
    (RetrieveRecordChangeHistoryResponse)_service.Execute(changeRequest);
    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)";
                        if (attributeDetail.OldValue.Contains(attribute.Key))

                       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!

How to connect to Dynamics CRM Onpremises AD/IFD from a Windows Form Application

Being a CRM developer, you often need to develop some external application. In this blog i will talk about and provide the code to connect to dynamics CRM Onpremises in a windows form application.

Login

public static ConnectToCrm(string username, string password, string domain, string Url)
{

ClientCredentials _clientCreds = new ClientCredentials();
_clientCreds.Windows.ClientCredential.UserName = username;
_clientCreds.Windows.ClientCredential.Password = password;
_clientCreds.Windows.ClientCredential.Domain = domain;

if (username == "" || password == "" || domain == "" || Url == "")
{
MessageBox.Show("Please Enter All Details To Login!");
}

_service = (IOrganizationService)new OrganizationServiceProxy(new Uri(Url), null, _clientCreds, null);

OrganizationServiceContext _orgContext = new OrganizationServiceContext(_service);
Guid orgId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).OrganizationId;

if (orgId != null)
{
MessageBox.Show("Successfully Connected to CRM. Click OK");
}
else if(orgId==null)
{
MessageBox.Show("Could Not Connect to CRM. Click OK & Try Again!");
}
}

The _service can be utilized in your methods to retrieve the data and perform actions in dynamics CRM. i.e, below:

var fetchExpression = new FetchExpression(fetchXml);
EntityCollection fetchResult = _service.RetrieveMultiple(fetchExpression);

hope this helps!

cheers!

How to get value from a lookup or parent entity using Calculated field in Microsoft Dynamics CRM

Did you ever need to get values from other entity or from a lookup on current entity? What comes to your mind, bet it is javascript , plugin etc?

Well, lets pause the coding life for minute and do it using Calculated Fields which used simple UI like business rules.

In Scenario below , i am retrieving the Budget Allocated amount from source campaign on a lead record.

  1. Create a currency Calculated field on lead record :1Give the appropriator name , select data type as Currency or as per your need > Select Field type as Calculated and click on Edit. As soon as you click on edit, system will actually create this field for you.
  2. After clicking on Edit button , you will be presented with the UI where you have put in  your logic:2

    As a first step i am checking if Budget Allocated field on campaign actually has data.To do this you have select Source campaign in the Entity and the corresponding field.

  3. Once this condition is set click on the Action :3

    In the editor start the typing the name of source campaign lookup or you can scroll though all fields/lookups available on the lead entity. Here i have select campaignid

  4. Once selected the campaignid , enter a dot(.) – post which you will start getting fields from the campaign:4

    Here i am selecting the budgetcost(Allocated Budget)

  5. Finally it will look like below :5Save and close the editor and also the field property window.
  6. Put this calculated field on the form and publish customization.
  7. Open a lead which has campaign associated to it:

    6campaign :

    7It is a very basic example i showed , i tried not to make it complex as it might become very confusing for a beginner.  You can do a lot manipulation with the data by using out of the box functions that are available for a calculated field or apply your custom logic.hope this helps!

    cheers!

RIP SDK – Dynamics CRM 365 v9

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.

Cheers!