locked
Plug-In Delete Message Parodox RRS feed

  • Question

  • I can't find any suggestions online so appolgies if this has been covered.

    I have two custom entities. Entity AA is the parent of Entity ZZ. Entity AA contains an integer value that contains the total value of all entity ZZ records. This is updated via a Plug-In when entity ZZ is created or updated.

    So far so go.

    When Entity ZZ is deleted I have a problem. Firstly I was using the Post Event for the Delete Message, then I realised that no data (other than the key) comes back, so I don't have parent entity AA's Id to do my recalculation.

    Switching to a pre-message will solve this problem, but this will mean the record will not yet have been deleted so my re-calculation routine will update the parent with the incorrect total. Is this correct?

    Do I need to then subtract the deleted entitys value from the total, or is there a set procedure for dealing with Plug-In Delete Messages that have parent entities that need follow up work?

    My Re-Calculation routine works in such a way that it doesnot have access to the Entity that fired it, when called only the Guid of the Parent is sent in and it then re-calculates based on the siblings detected.

    Many thanks

    Steve

    Tuesday, June 2, 2009 12:09 PM

Answers

  • register an pre-image to your post plugin.
    and then the image can be retrieved by DynamicEntity preimage = (DynamicEntity)context.PreEntityImages["imangename"];
    ((Lookup)preimage.Properties["parentid"]).value will give the Guid of the parent entity.
    If this answers your question, please use the answer button and say so. | Anoop
    • Proposed as answer by Anoop Kheerwal Tuesday, June 2, 2009 5:22 PM
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 3:47 PM
  • Hi
       On PreEvent you can get parent guid and put it in SharedVariables like this
    context.SharedVariables.Properties.Add(new PropertyBagEntry("someID", someguid));

       Then in PostEvent you can get it like this
    Guid someguid = (Guid)context.SharedVariables["someID"];

       And use it.
    everything is a matter of probability...
    • Proposed as answer by Adi Katz Tuesday, June 2, 2009 1:01 PM
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 12:35 PM
  • Check context.Stage property. Enum for this is MessageProcessingStage. Here is a little code

    if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
    {
        //PreEvent
    }
    if (context.Stage == MessageProcessingStage.AfterMainOperationOutsideTransaction)
    {
        //PostEvent
    }

    EDIT:
    Message name you can check like this

    if (context.MessageName == MessageName.Delete)
    {
        //Delete message
    }
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 2:05 PM

All replies

  • Hi Steve,

    In your delete message, once you get the Id of the record being deleted. You can create a CRM service instance using Plugin Context. Using that CRM service instance you can retrive the the parent id of the current reecord being deleted and do the calculation.
    Regards,
    Chinmay
    http://metrix.blogspot.com
    Tuesday, June 2, 2009 12:19 PM
  • Hi Chinmay

    I can get the key of the parent, but as this is now a pre-event the record will not be deleted yet, so my Re-Calculate routine will still find the to-be-deleted record will it not?

    I'm unsure on Delete Processing, but I think I need to get the GUID then let the record be deleted and then update the parent, but I'm unsure how to go about this during the context of the initial delete message.

    Cheers

    Steve
    Tuesday, June 2, 2009 12:28 PM
  • Hi
       On PreEvent you can get parent guid and put it in SharedVariables like this
    context.SharedVariables.Properties.Add(new PropertyBagEntry("someID", someguid));

       Then in PostEvent you can get it like this
    Guid someguid = (Guid)context.SharedVariables["someID"];

       And use it.
    everything is a matter of probability...
    • Proposed as answer by Adi Katz Tuesday, June 2, 2009 1:01 PM
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 12:35 PM
  • That's really cool, I'll give it a go and update you all on the results.
    Cheers

    Steve
    Tuesday, June 2, 2009 1:17 PM
  • My Plug-In routes to the same class, how do I know if the context is a Pre-Delete or a Post-Delete as I don't see that message in the context properties?

    Regards

    Steve

    Tuesday, June 2, 2009 1:59 PM
  • Check context.Stage property. Enum for this is MessageProcessingStage. Here is a little code

    if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
    {
        //PreEvent
    }
    if (context.Stage == MessageProcessingStage.AfterMainOperationOutsideTransaction)
    {
        //PostEvent
    }

    EDIT:
    Message name you can check like this

    if (context.MessageName == MessageName.Delete)
    {
        //Delete message
    }
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 2:05 PM
  • Hi Steve,

    Easiest way to go with Post Plugin. You can still add a pre image to the post plugin. Retrieve parentid from the pre image and update the total count accordingly.
    Sure there are other ways to do this as suggested above, but why to go into much sophistication/complication if the same goal is achieved by much simpler approach.

    Thanks
    Anoop


    If this answers your question, please use the answer button and say so. | Anoop
    Tuesday, June 2, 2009 2:20 PM
  • Hi Anoop

    How does the pre-image work as when I looked at the data within the Post-Delete context all I get back is the moniker for the record that's been deleted. I started down the Shared Variable route as I need to capture the parent guid the record is associated to.

    Is there a way of passing this information during the registration of the Post-Delete event?

    Cheers

    Steve

    Tuesday, June 2, 2009 3:31 PM
  • Thanks for this I've coded this in my Plug-In. However the Pre-Delete event also only returns a Moniker. I though it would of returned all the properties!

    Is there a quick way of retrieving the GUID of a related record during my Pre-Delete event when one is only armed with a Moniker?
    Many thanks

    Steve
    Tuesday, June 2, 2009 3:33 PM
  • register an pre-image to your post plugin.
    and then the image can be retrieved by DynamicEntity preimage = (DynamicEntity)context.PreEntityImages["imangename"];
    ((Lookup)preimage.Properties["parentid"]).value will give the Guid of the parent entity.
    If this answers your question, please use the answer button and say so. | Anoop
    • Proposed as answer by Anoop Kheerwal Tuesday, June 2, 2009 5:22 PM
    • Marked as answer by lemonje Wednesday, June 3, 2009 11:18 AM
    Tuesday, June 2, 2009 3:47 PM
  • Thanks Anoop

    Sorry for being a bit vague. I've registered plug-in steps over the last four weeks and assumed the pre and post images were automatic. I didn't realise one had to register then directly. I've now see that step in the Plug-In registration tool and are now researching what I can do with these images.

    Many thanks

    Steve
    Tuesday, June 2, 2009 4:03 PM
  • Hi Anoop

    I've now had time to try both these techniques.

    a. Shared Variables; storing the guid on the Pre-Delete and picking it up again on the Post-Delete
    b. Loading the Guid in the Pre-Image of the Post-Delete

    I really like the Shared-Variable approach for ingenuity and I'm sure I'll use this in the future, but the pure simplicity of the pre-image on Post-Delete solves all my problems with the least amount of code, so that's the way I'm going.

    Many thanks to all contributors.

    Steve

    Wednesday, June 3, 2009 11:17 AM