Marqueone
Marqueone

Reputation: 1213

CRM online plugin fails to execute the same way as when debugged

I've just finished writing a plugin for crm online, and am now running into a curious problem.

When I run the plugin through the debugger I get the expected results, but when I let the plugin run on its own it fails to run as expected.

In this plugin, I'm listening to the create event for new queueitems and I'm checking to see if the queueitm is an email. Everything up to this point is running as expected, however when after getting an email I check to see if the email has an attachment and here's when things start deviating. Through the debugger I can see the attachment file, but on its own, the plugin can't find the attachment. This is very odd to me because I can see the email in the queue and there's definitely an attachment there.

What would cause this to happen, why does it happen and is this a common problem?

Upvotes: 1

Views: 167

Answers (1)

James Wood
James Wood

Reputation: 17552

Bit of guess.

There is a timing issue, when you debug and that timing issue isn't apparent because you have artificially slowed the application down.

I'm guessing your plugin is sync, and things are happening like this.

In Execution

  1. Email created.
  2. Thread starts to add attachment to email.
  3. Your plugin runs, and finds no attachment.
  4. Thread adds attachment and finishes.

In Debugging

  1. Email created.
  2. Thread starts to add attachment to email.
  3. Your plugin runs, and goes to breakpoint.
  4. Thread adds attachment and finishes.
  5. You inspect the property and find the attachment.

Given that the email and email attachment are separate database tables this seems like a good shout, CRM may be making the two create calls separately.

Ways to test this theory

  1. Make your plugin async.
  2. Add a Thread.Sleep into your code (no more than 2 minutes, a minute should be fine).
  3. Find another place or step to register your plugin.
  4. Stick a plugin on the email attachment create which throws an exception to see what order things happen in.

If my theory turns out to be true, the best resolution is to probably find another way to register your plugin.

Upvotes: 2

Related Questions