Using Update to change the status and update owner (instead of SetState and Assign Request) in CRM 2015/2016

Prior to CRM 2015 Update 1, if we had to change the owner or set the state of the record we had to use Assign and SetState Request.

Now we can use our Update request for the same.

For e.g. we have below Active record owned by user named Nishant Rana.

Using below Update Request we can change the owner as well as set the record as inactive in a single request.

Entity demoEntity = new Entity("new_demoentity");

// here we are using alternate key to update the record instead of Guid
demoEntity.KeyAttributes.Add("new_alternatekeyfield", "My Alternate Key 2");

demoEntity.Attributes["new_name"] = "Updated record at " + DateTime.Now.ToShortTimeString();

// update owner id
demoEntity.Attributes["ownerid"] = new EntityReference("systemuser", new Guid("16406B31-5E97-E611-80E3-FC15B42877A8"));

// set record as inactive
demoEntity.Attributes["statecode"] = new OptionSetValue(1);



Point to remember is that if we have plugin registered either PRE or POST on Update and Assign both will be triggered.

Hope it helps..

Sample Code to use UpsertRequest in CRM 2015\2016

UpsertRequest was a new request introduced in CRM 2015 Online Update 1.

The request based on the alternate key specified, looks for the record, if the record is existing it updates it else it creates a new record.

UpsertResponse has a property named RecordCreated which is false if the record is found and updated else it is true if record is created. The Target property holds the reference of the record created or updated.

Suppose we have following record created. It has Alternate key defined on “Alternate Key Field”

Sample Code

As we have demo entity record with alternate key field having value as “My Alternate Key 1” it will update the record.

Running it again this time changing the value for the alternate key field.

It creates a new record.

Hope it helps..

Using PluginTypeStatistic records to monitor plugins \ custom workflow activities hosted in sandbox in CRM 2016 (and earlier)

CRM collects runtime information about the plugins and custom workflow activities executing in sandbox mode in PluginTypeStatistic records every hour or so.

Go to Advanced Find View and select – Plug-in Type Statistics

It gives us details regarding “The average execution time”, “Execution Count”, “Failure Count” etc. for each of the plugins and custom workflow activities.

Hope it helps.

Alternate Keys in CRM 2015\CRM 2016

Let us look at a simple example to understand.

  • Suppose we have a custom entity name Demo Entity.
  • Create a new single line of text field named Alternate Key Field.
  • Create a new Alternate Key Field and specify the above field for it.

  • Click on “Create index for My Alternate Keys for entity Demo Entity” which will create an Index in CRM’s Database.
  • Let us update the following record using Alternate Key

  • Sample code

  • Updated record

  • Currently alternate key can only be defined on field type – string, integer and decimal.
  • The alternate key can be used for Entity Reference as well, wherein instead of specifying GUID we can now use alternate key.

Hope it helps..

Plug-in and Custom Workflow activity tracing in CRM 2015\CRM 2016.

To configure it,

Go to Settings – Administration – System Settings

Here we have selected “All” for enable tracing and registered a sample plugin which only writes trace.

Now perform the event that will trigger the plugin.

Go to Settings – Plugin Trace Logs

Open the Trace Log record

Trace message

In case of enable logging set as Off no trace log records will be created and in case of Exception only when exception occurs trace log record is created.

“Trace logs older than 24 hours are deleted by Bulk Deletion background job”

Hope it helps..

Using RetrieveEntityChangeRequest to track changes in CRM 2015 / 2016.

As a first step,

Enable Change Tracking for the entity, custom Demo Entity in our case here.

Suppose we have 5 records created for Demo Entity.

As we are calling it for the first time DataVersion is set as empty.

We got all the 5 records as new or updated item.

Save the DataToken to be used while determining the changes and to be passed next time.

Now let us delete Record 1 and Record 2 and update Record 5 and run the code again this time passing the DataToken saved

As expected we get 3 changes.

Now suppose we create a new record name Record 6 and still use the old Data Token

As expected we got 4 changes here, which includes the new record 6 as new or update.

Point to remember here is if no version is passed the system will return all the records as new, and system will consider records that are within 90 days, if it is older than 90 days all the records will be returned.

If we have a new record created and deleted before we retrieved for the changes and the record didn’t exist at the time of retrieval, we will still get the record as deleted.

Sample Code

EntityCollection newOrUpdatedEntityCollection = new EntityCollection();
EntityReferenceCollection removeOrDeletedEntityRefCollection = new EntityReferenceCollection();

RetrieveEntityChangesRequest retrieveEntityChangeRequest = new RetrieveEntityChangesRequest();
retrieveEntityChangeRequest.Columns = new ColumnSet();

// pass DataVersion
retrieveEntityChangeRequest.DataVersion = "1018501!10/24/2016 12:31:46";

retrieveEntityChangeRequest.EntityName = "new_demoentity";
retrieveEntityChangeRequest.PageInfo = new PagingInfo();

RetrieveEntityChangesResponse retrieveEntityChangesResponse = (RetrieveEntityChangesResponse)organizationProxy.Execute(retrieveEntityChangeRequest);

foreach (var entity in retrieveEntityChangesResponse.EntityChanges.Changes)
var dataToken = retrieveEntityChangesResponse.EntityChanges.DataToken;

if (entity.Type == ChangeType.NewOrUpdated)
else if (entity.Type == ChangeType.RemoveOrDeleted)

Hope it helps ..

%d bloggers like this: