locked
Retrieve attachments from E-mail router messages never gives results on Post-Operation message RRS feed

  • Question

  • Hi there,

    Have a strange issue and I hoped that someone can help.
    We have a CRM Queue with an assigned email address in our CRM system. In this queue, emails with XML attachments are received and I'm now creating a plugin to process the XML attachments.

    I created a Post-Operation plugin on the "Create" message for the Queueitem entity.  In my code I check if the new queue item is an email and then I use following code to look up all the attachments:

    public EntityCollection GetAttachments(this IOrganizationService pService,  Guid pInput)
            {
                QueryExpression qxprAtt = new QueryExpression();
                qxprAtt.EntityName = "activitymimeattachment";
                qxprAtt.ColumnSet = new ColumnSet(true);
    
                ConditionExpression cxprAtt = new ConditionExpression();
                cxprAtt.AttributeName = "activityid";
                cxprAtt.Operator = ConditionOperator.Equal;
                cxprAtt.Values.Add(pInput);
    
                FilterExpression fxprAtt = new FilterExpression();
                fxprAtt.Conditions.Add(cxprAtt);
    
                qxprAtt.Criteria = fxprAtt;
    
                return pService.RetrieveMultiple(qxprAtt);
    
            }

    When an email is received for that queue using the Exchange email router, the create queueitem plugin gets fired but the above code returns 0 attachments. If I open the mail in CRM, the XML attachment is there.

    As a test I manually created a new email in CRM and added an XML as attachment.  Then I added this email to the CRM queue using the email form button "add to queue" and now the above code returns 1 and the email gets processed correctly.

    Anyone knows what is happening here?

    • Edited by Neburoner Tuesday, September 24, 2013 7:53 AM
    Tuesday, September 24, 2013 7:48 AM

All replies

  • I expect the problem is due to the sequence in which data is written to CRM. When an email with an attachment is received via a queue, 3 (there are more, but only 3 matter in this case) records are created:

    • An Email
    • A QueueItem
    • An Attachment (note)

    I suspect that these records are created in the order listed, so when your plugin fires on the QueueItem, the Attachment has yet to be created. Whereas, when you do the manual test, you've added the attachment before the QueueItem is created.

    There won't be a way to modify the record creation sequence. One thing you can try is to set your plugin to run asynchronously; however I'm not sure if you will be able to guarantee that the attachment will have been created, as I expect that is part of a separate transaction


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, September 24, 2013 4:00 PM
    Moderator
  • Hi David,

    Thanks for your response.  I already expected something like that but I'm wondering what's the best way to get around this. I've got a few ideas, what do you think about it?

    1. Would introducing a 15 sec "sleep" in my queueitem plugin be a good idea on an async plugin?
    2. I could create a plugin on "Create" of the entity "activitymimeattachment" and check if the email is in the target queue

    Are any of these good options?


    • Edited by Neburoner Wednesday, September 25, 2013 7:22 AM
    Wednesday, September 25, 2013 7:22 AM
  • Anyone else that has some experience with it and can tell me what's the recommended way to get around this?
    Tuesday, October 1, 2013 9:34 AM