Jun 11, 2015

Exception handling – Process failed status in Asynchronous process

We discussed about handling of exception of plug-ins in few occasions. We also discussed and   recommended the usage of tracing mechanism and you can check it here.

It could be important to show a formal message to the user if a process is failed, especially in Asynchronous processes because it doesn’t throw any exception as in synchronous processors. User may not have sufficient permission to check system jobs either. Tracing information is not for end users, but for developers. In this case, we can use a custom status field with one value as process failed.

Please check the code snippet and notice how that status change happened just before exception is thrown.

try
{
  //Any information you need to trace
  // ex;
  //tracer.Trace("Account:{0} and Guid:{1}", _account.Name, _account.accountid);
}
catch (Exception ex)
{
  // ***status change to Failed status ****
  throw new InvalidPluginExecutionException("[" + ex.Message + "]" + ex.StackTrace, ex);
}
finally
{
}

Now, this worked fine for lengthy processes for me. Yet, there is one exception: if issue occur with SQL time out/ DB connection this will become a problem. Why? Simply this update won’t happen and system will stuck in that point… bitter truth is we are not getting any trace information either, because it come after this step.

So you need to decide depending on your scenario. If the SQL time out is a possible culprit, just forget about this status change.

Mar 31, 2015

Many–to-Many Associate, Disassociate and Retrieve

This is how you associate many-to-many relationships programmatically. One good thing about this approach is you can associate more than one record in one call. Disassociate too takes same parameters.

AssociateRequest request = new AssociateRequest();

EntityReference _ref1 = new EntityReference(<entity A - logical name>, <entity A - Guid>);
request.Target = _ref1;

EntityReferenceCollection _entRefColl = new EntityReferenceCollection();
_entRefColl.Add(new EntityReference(<entity B - logical name>, <entity B - Guid 1>));
_entRefColl.Add(new EntityReference(<entity B - logical name>, <entity B - Guid 2>));
request.RelatedEntities = _entRefColl;

request.Relationship = new Relationship("<relationship name>"); 
//relationship name ex: new_account_office

crmService.Execute(request);

Now one important fact;

Intermediate table of the many-to-many table is having the name of the relationship. For ex: new_account_office. This table got two Guids which are the primary key values of the two respective records to be associated which is obvious.

You are allowed to query this intermediate table as you wish. One thing to keep in mind is two Guids coming out of this table are purely Guids and NOT Entity References.

Probably, this is the only occasion you retrieve Guid from a table which are not the Primary key in CRM.  All other entity retrievals, if not a primary key we are retrieving Entity Reference type.  Clear?