Thursday 14 July 2016

CRM Tracing/Debugging Service

When writing plugin we try to make sure all possible scenarios are covered however when application is deployed at client side they enter some data which we didn't think of when writing code or use application which causes Business Process error BPE, In this case CRM shows BPE dialog window and use can click on download log button to view the stack trace, sometimes that's not enough, and we should use tracing service so that we can trace to be bit more understandable about which code been executed and with what values been passed by showing including that to trace.

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

then use the Trace method to write trace

tracingService.Trace("Cost provided"+ entityObject.prefix_fieldname);

Now when plugin throws BPE we will get more of an idea about what happened in code.

--
Remote Debugging

Go to google and seach for Remote tools download and install it on the machine which needs to be debugged, if this is a UAT environment within same network where the bug is reported and not able to reproduce locally, Then install it on their. Either to start and stop remote debugger go to start and then select Remote debugger windows app Or else go to windows services area and set the Remote debugger service to run as always.

Run the Remote debugger under local system account.



Depending how the plugin is registered we need to attached to respective process e.g. if the plugin is registered to run under sand box then we need to attach to sandbox process or in case of custom assemblies sandbox async service. If plugin is set to run under full privilege (none/not sandbox) then select w3wp for sync plugin or async service for customer assemblies or async plugins.

To debug with in visual studio go to debug -> attach to process
Attach to : Automatic: Native code (when service to attach is selected then the value might change to Automatic)
on qualifier field click on find and it will discover any remote debuggers running with in local network/subnet, or type manually default to 4016 port

Then trigger the process to debug the code (debug sysmbole won't load initially unless they are triggered an cached first time) if the symbols still doesn't load, then look if there are any more process pending to attached if there are then attached them e.g. w3wp or aysnc service, if this isn't the case cancel attached process and rebuild solution and redeploy and do the attach to process again.

Please note there is default 2 mins time out for Sandbox CRM process to terminate and restart if process take long to process


Different ways of doing converting list to custom arrays


List<EntityReference> list = new List<EntityReference> { new EntityReference("contact", Guid.NewGuid()), new EntityReference("contact", Guid.NewGuid()) };

 
Option 1


Entity[] listArray = new Entity[toList.Count];

 

for (int i = 0; i < toList.Count; i++)

{

Entity toParty = new Entity("activityparty");

toParty["partyid"] = toList[i];

listArray[i] = toParty;

}


Option 2
 
Entity[] listArray = new Entity[toList.Count];


list.ForEach(delegate(EntityReference item)

{

Entity ccParty = new Entity("activityparty");

       ccParty["partyid"] = item;

       listArray.SetValue(ccParty, listArray.Count());

});


Option 3
 

Entity[] listArray = ccList.ConvertAll<Entity>(delegate(EntityReference item) { return new Entity { LogicalName = "activityparty", Attributes = { { "partyid", item }   } }; }).ToArray();