Armanino Blog
Article

Adding Logic When Quotes are Activated in Microsoft CRM

October 04, 2013

Recently, I needed to write a Dynamics CRM plug-in to roll up the total amount for all quotes into their parent entity. The plug-in was to fire whenever a quote was activated. I knew from past experience that the message corresponding to quote activation was SetState, so I registered the plug-in accordingly. However, it absolutely refused to fire.

I received no error messages, and my attempts to debug it were a waste of time. It seemed very straightforward, so I wondered if there was a simple “gotcha” that I was missing?

Eventually, I tracked down an old blog entry (written by David Fronk at Dynamic Methods) that provided valuable illumination. Apparently, if a plug-in needs to react to the SetState message, it must also be registered against the SetStateDynamicEntity message. Furthermore, I couldn’t just register that new step—I also had to unregister my plug-in assembly, then re-register it.

After passing that hurdle, I realized that the plug-in would also need to fire whenever a quote was closed (lost or cancelled). Therefore, I also registered the plug-in against the Close message.

Here is a code snippet showing the plug-in logic:
if ( (context.MessageName == “SetStateDynamicEntity”&& context.InputParameters.Contains(“EntityMoniker”)) ||

     (context.MessageName == “Close” && context.InputParameters.Contains(“QuoteClose”)) )
{
   Guid quoteId;
   // If the user closed a quote, fetch the quote’s Guid from the QuoteClose entity.
   if (context.MessageName == “Close”)
   {
      Entity quoteCloseEntity = (Entity)context.InputParameters[“QuoteClose”];
      quoteId = ((EntityReference)quoteCloseEntity[“quoteid”]).Id;
   }
   // If the user changed the status, fetch the quote’s Guid from the EntityMoniker reference.
   else
   {
      quoteId = ((EntityReference)context.InputParameters[“EntityMoniker”]).Id;
   }
                   
   Entity quote = service.Retrieve(“quote”, quoteId, new ColumnSet(true));
   // Proceed…
}


Stay In Touch

Sign up to stay up-to-date with the latest accounting regulations, best practices, industry news and technology insights to run your business.

Resources
Related News & Insights
IPO Prep & SOX Compliance: Instacart & Armanino Share Hard-Earned Knowledge
Webinar
Know your compliance requirements today to avoid obstacles tomorrow.

December 16, 2021 | 11:00 AM - 12:00 PM PT
General Contractor Trends to Consider in 2022 Webinar
Webinar
Hear from experts how you can better manage your subcontracts — and more.

December 16, 2021 | 10:00 AM - 11:00 AM PT
Why COP26 Matters for Your Business Webinar
Webinar
Get informed of COP26 developments and how you can act now.

December 15, 2021 | 10:30 AM - 11:00 AM PT