Plugin was caught in a recursive loop

I was trying to write a plugin which updated a counter and validated some values.

I wasn’t entirely sure what I was doing, so amusingly in my plugin I created an organisation and then did a create to update the entity.

so I was changing the value of a field, counter + 1

calling a Organisation.Create(entity)

this caused my plugin to be fired again! and again and again and again.  I had turned on tracing and I could see in the log file it was looping over.  I had also turned on tracing in the plugin, which you can read about here in a previous blog post.

I think I also had the plugin being fired in the wrong stage of the pipeline.  Initially I had it in Post-operation but then I changed this to Pre-operation.  It gets confusing because it’s pre update of the entity, which is what I actually wanted because I wanted to adjust some of the values before they are all written back to the database.

Post-operation is after the update has happened.

I also confirmed the default number of loops in a plugin.  It is set to 8 and I know this because it is one of the questions on the Extending CRM 4 certification.  Another question is does CRM automatically catch recursive loops, the answer is also yes (and good job otherwise I would have been stuck staring at my screen wondering what was happening).  I always like to reinforce the usefulness of the CRM exams, so when things I have learnt in the exam pop up in every day usage, I give myself a pat on the back



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.