Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRM 2011 Workflow "Invalid Pointer" error

I have a custom workflow in CRM 2011 which is manually triggered against custom entity records. When a single record is selected for processing, the workflow is always successful. However when selecting more than one record, at least one will fail. The error(s) provided seem to vary from one attempt to the next, even though the same data is being used.

Errors I've encountered so far are:

  • ValidateOpen - Encountered disposed CrmDbConnection when it should not be disposed You cannot create a SqlExecutionContext from another
  • SqlExecutionContext on which OnBeginRequest has not been called

And after restarting the DB server:

  • Invalid Pointer

All of these occur when calling the Update method of the IOrganizationService. The Invalid Pointer error seems to be the more common error since having restarted the DB server (it was suggested to me that it might be an issue with an overly-busy DB server). I've also deleted any backlog of asynchronous tasks in case this was the issue, but it hasn't had any effect.

Does anyone know what this error means, why I might be getting it or how I can get around the issue?

Many thanks!

like image 729
Alec Avatar asked Feb 13 '12 17:02

Alec


1 Answers

It seems this error message is a round-about way of saying, 'tried to open another connection to CRM when the previous one was open', and the reason it tries to do this is very much relevant to the fact it only fails when processing more than one record.

Reference this blog:

It seems that when a workflow is run against multiple records, it uses the same instance of the class, which means that class-level variables won't be re-instantiated between executions. So, when subsequent executions come to the code which sets the class-level service variable to an instance of an IOrganisationService, it finds the variables already has one and that it's open.

The solution I've found easiest to implement it to have the service variable within the Execute function, rather than class-level. This has solved the problem everywhere that I've tried it since.

like image 106
Alec Avatar answered Oct 02 '22 00:10

Alec