Dynamics CRM Exceptions
Welcome to another exciting article on Dynamics CRM. In this article we will discuss some common exceptions of Dynamics CRM and the ways to deal with these exceptions.
While developing Dynamics extended projects like Plugins, Workflows, Batch jobs etc we face some exceptions in run-time.
Errors in Dynamics CRM development & Solutions.
The given key was not present in the dictionary.
CAUSE:This error comes when you are reading a value of a key or attribute value from a entity attribute collection or from a .net dictionary, but the attribute is not present in the collection.
For example if you are reading a value as below,
Entity ent = context.InputParameters["Target"]; var contracttype = ent.Attributes["new_contracttype"];
If the field new_contracttype value is null then the entity collection will not have this field in the collection.
DIAGNOSE:If you have taken multiple fields then better to debug the code to understand which specific field throwing error Otherwise use Advance find or SQL query to identify which field value is blank and then change the code as given in resolution below.
RESOLUTION:Check if the key is present in the collection then use it. Change the above code like below.
var new_contracttype = null; if(context.InputParameters.Contains("Target")) { Entity ent = context.InputParameters["Target"]; if(entity.Attributes.ContainsKey("new_contracttype")) { contracttype = entity.Attributes["new_contracttype"]; } }
NOTE:
- In plugins, the values in context.InputParameters and context.OutputParameters depend on the message and the stage that you register the plugin on. For example, “Target” is present in InputParameters for the Create and Update messages, but not the SetState message. For SetState message its “EntityMonikor”. Also, OutputParameters only exist in a Post stage, and not in a Pre stage.
- The Attributes collection of an Entity will only contain values for attributes that have a value. You may get the Entity from a Retrieve or RetrieveMultiple having specified a ColumnSet with the attribute you want, but this attribute will not be present in the Attributes collection if there were no data in that attribute for that record.
- In plugins, the Attributes collection of an Entity that you obtain from the “Target” InputParameter will only contain attributes that were modified in the corresponding Create or Update method. Using the example above, if this were in a plugin registered on the Update message, the “name” attribute would only be present if the “name” attribute was changed as part of the Update; the “Target” InputParameter will not contain all the attributes for the entity. If you want to get the values in the Target although the field values are not changed then you have to use Entity Images. Read more here on Plugins. Read how to handle exceptions in plugin codes.
The caller was not authenticated by the service.
CAUSE: This error comes when you are calling CRM Service with a wrong credential or domain.
DIAGNOSE: Check the connection string you are declaring is correct or not.
RESOLUTION: Use the simplified code to call CRM Service
ClientCredentials creds = new ClientCredentials(); // Credentials creds.Windows.ClientCredential = new System.Net.NetworkCredential(userName, password, domain); OrganizationServiceProxy service = new OrganizationServiceProxy(new Uri(orgURL), null, creds, null); service.Timeout = new TimeSpan(0, 5, 0); return (IOrganizationService)service;
Assembly must be registered in isolation
CAUSE:This error comes when you are trying to register or update a plugin but you are not added as a Deployment Administrator.
DIAGNOSE: Open Deployment Admin and check if you are added as an user or not.
RESOLUTION: Add your user id in deployment admin group and try again registering the plugin or updating.
NOTE: For online only the plugins are must be registered in isolation mode.
Generic SQL Error – A SQL Server error occurred
CAUSE: This error has many causes. May be the DB size is full. May be the SQL query is high time consuming. May be the plugins and workflows are build in a poor coding. So it requires a proper diagnostic to understand the cause.
DIAGNOSE: Check the event viewer (Run->cmd->eventvwr) to identify more details from the log. If the event viewer is not giving more information then Enable CRM Tracing. But be careful while enabling CRM Tracing because it will make the system slow.Sometimes its difficult to reproduce this type of issue because it may be happening in production environment where the data flow is higher.
Check if the SQL Organization database size is full or if there is any limit is implemented. Increase the OLEDBTIMEOUT registry key. Most importantly we have to identify the deadlocks.
Observe the timing of more deadlocks in SQL end and contact DBA to prepare a report to understand why at this time only the deadlocks are coming, is there any batch job or complex report running which read or writes value in CRM or is there dynamics connector is implemented to flow huge data from or to CRM and ERP.
RESOLUTION:The resolution is not straight forward. Follow the below steps to resolve the issue. Chek this article to optimize and identify this issue.
- Check Event Viewer to read more info on that exception. (Run->cmd->eventvwr)
- Enable CRM Tracing to identify if there is any deadlock in SQL or Using SQL Profiler.
- Increase OLEDBTimeOUT registry key
- Most importantly Optimize the custom codes plugins, batch jobs, workflows etc. refer here.
- Contact SQL DBA and extract a SQL deadlock statistics report to identify the timing when the most deadlocks happens.
- Identify what processes are running parallel. Change the timing of batch jobs out of the business hour.
- Update and Rebuild SQL indexes periodically.
- Clean up the POA(Principal Object Access) table periodically.
- Clean up the async operation/system jobs logs.
- Monitor the event logs to identify the long executing threshold sql queries and optimize the code by identifying the custom code using the equivalent to sql query.
NOTE: Most important is to optimize the custom codes you write like plugins, custom workflows batch jobs etc.
Error registering plugins and/or workflows. Plug-in assembly does not contain the required types or assembly content cannot be updated.
CAUSE:This error will come if you are trying to update an existing plugin with a different name by another plugin.
DIAGNOSE:Open Plugin registration tool and compare the names of existing plugin class name and new plugin names.
RESOLUTION: Update the correct plugin or create new plugin if the plugin is not yet registered.
If you know any issue please add in comment.. Thanks for reading this article.
Read the below articles worth reading related to this article.