How to Execute an Operation Outside of a Plugin Transaction in Dynamics CRM

1.1k views Asked by At

I have a Trace entity in my CRM system, and I want to insert records for that entity regardless of whether or not a plugin or workflow activity fails. When real-time plugin/workflow fails, all the data operations that have happened are rolled back, so any inserted Trace records are also rolled back.

I know I can use the ITracingService, but there are many times when end users aren't willing/able to relay the issues on to me when an error occurs.

Does anyone know of a supported way to have the Trace records get inserted outside of the transaction, so they are available whether or not the code succeeds or fails?

1

There are 1 answers

4
Nicknow On BEST ANSWER

If you are using Dynamics CRM Online (this is now supported as of the Spring Update.) It works great and automatically clears the log after 1 day.

For on-premises, you have to write them outside of the executing transaction. So, you have a few different options:

  1. Create a new connection to Dynamics CRM using stored credentials (store them in a custom entity or the Plugin Step's Unsecure or Secure Configuration String (recommend you encrypt the password whichever option you choose.)

  2. Use a tool such as NLog (or Log4Net) and log the same as any other .NET application (since online & sandbox are not a limitation.)

  3. Roll your own custom logging solution and write to either disk or a webservice. Make it implement ITracingService (it just has one simple method) and it can be used anywhere you might use CRM's tracing implementation. You can see my quick implementation (for a different purpose) here: https://stackoverflow.com/a/28186429/394978. You'll still need to implement the writing to disk/webservice/sql. Btw, make it write to a web service and technically it could still work in CRM Online.