none
Plugins, Messages, and StateCode access

    Question

  • This is in a 2011 CRM Online instance.

    We currently have post-operation plugins registered on the QueueItem Entity for the SetState and SetStateDynamicEntity messages. When the related Entity's state, in this case an opportunity, forces the QueueItem to an Inactive state the plugins are not firing. We have removed all code and thrown exceptions immediately in the plugins just to confirm that it is in fact completely skipping the firing of these messages in this use case.

    The Update Plugin I registered Post-Operation is firing however. To verify, I've filtered this plugin down to just the StateCode in the filtering Attribute. It still fires on the QueueItem when you close the related opportunity with a Won/Lost.s

    The problem is that it will not allow me to access the StateCode from the Context or the Post Image Alias. I get  the following exception when I pull the StateCode:

    Entity QItem = (Entity)context.InputParameters["Target"];
    
    localContext.TracingService.Trace("Retrieving postState");
                                int postState = ((OptionSetValue)QItem.Attributes["StateCode"]).Value;
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220956</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>Unexpected exception from plug-in (Execute): GracoCustomizations.Plugins.PostQueueItemUpdate: System.Exception: Error in PostQueueItemUpdate Plug-in:The given key was not present in the dictionary.</Message>
      <Timestamp>2013-06-28T21:00:14.3469294Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>
    
    [GracoCustomizations.Plugins: GracoCustomizations.Plugins.PostQueueItemUpdate]
    [7319d0df-8db7-e211-8a81-78e3b508f865: PostQueueItemUpdate]
    
    Entered GracoCustomizations.Plugins.PostQueueItemUpdate.Execute(), Correlation Id: f6dd58f7-81ed-4313-bef5-eec72574f2d1, Initiating User: cfad9426-55a1-45a3-8f5f-558da43a8d07
    GracoCustomizations.Plugins.PostQueueItemUpdate is firing for Entity: queueitem, Message: Update, Correlation Id: f6dd58f7-81ed-4313-bef5-eec72574f2d1, Initiating User: cfad9426-55a1-45a3-8f5f-558da43a8d07
    Recieved QueueItem Context. 
    Recieved Org Service. 
    Should be an Oppt: 3
    Preparing to Assign Pre UserIDs: a81d9444-2ae0-e211-8985-78e3b508f865
    Preparing to Assign Post UserIDs. a81d9444-2ae0-e211-8985-78e3b508f865
    Retrieving postState
    Exiting GracoCustomizations.Plugins.PostQueueItemUpdate.Execute(), Correlation Id: f6dd58f7-81ed-4313-bef5-eec72574f2d1, Initiating User: cfad9426-55a1-45a3-8f5f-558da43a8d07
    
    
    </TraceText>
    </OrganizationServiceFault>

    Is this expected behavior or did we miss something? We have other plugins registered for state change messages that fire even when the change is initiated from an Opportunity, it seems on the surface that these should fire as well. Also, what is up with the StateCode in an Update that fires specifically on a statecode change? We are fine with handling it either way if someone has a solution to either problem.

    Thanks for any guidance on this issue.

    Jason


    Friday, June 28, 2013 9:09 PM

Answers

  • Hi,
    you are using latebound, so first is to lower case the attribute name, so you can try with:

    int postState = ((OptionSetValue)QItem.Attributes["statecode"]).Value;
    and good habit is to check if the entity contains first the attribute
    if (QItem.Attributes.Contains(statecode)) {
       int postState = ((OptionSetValue)QItem.Attributes["statecode"]).Value;
       // ...


    My blog: www.crmanswers.net

    Friday, June 28, 2013 9:40 PM

All replies

  • Hi,
    you are using latebound, so first is to lower case the attribute name, so you can try with:

    int postState = ((OptionSetValue)QItem.Attributes["statecode"]).Value;
    and good habit is to check if the entity contains first the attribute
    if (QItem.Attributes.Contains(statecode)) {
       int postState = ((OptionSetValue)QItem.Attributes["statecode"]).Value;
       // ...


    My blog: www.crmanswers.net

    Friday, June 28, 2013 9:40 PM
  • Well, don't I feel like an idiot. I've been staring at that for over a day and completely missed it....

    And I agree, I always use contains. I had stripped everything away testing different possibilities.

    Thank you for pointing my typo out.

    Jason

    Friday, June 28, 2013 9:53 PM