locked
CRM 2011: How to find dirty fields in plugin update using post image? RRS feed

  • Question

  • Hello,

    I have a plugin runs update and I have a post image for this plugin. In my plugin, I use late bound. I want to find some dirty fields value(the fields value is changed after update) to compare the difference before and after updating.

    For example, I was trying to use following code to get dirty fields value, but I found they're same, so what's wrong with my code? How can I get these fields value(before and after updating)?

    Entity updateEntity = (Entity)this.context.InputParameters["Target"];
    Entity postEntity = (Entity)this.context.PostEntityImages["Target"];
    bool dirtyFieldCurrent = updateEntity.GetAttributeValue<bool>("dirtyfield");
    bool dirtyFieldPOST = postEntity.GetAttributeValue<bool>("dirtyfield");

    'dirtyfield' is the one should changed when running update, but I found in my code dirtyFieldCurrent is same as dirtyFiledPOST.

    Any help will appreciate.

    Thanks.

    Friday, October 25, 2013 7:42 PM

Answers

  • Hi,

    The PreImage is not the same as the Target. The PreImage will contain the values you request in the registration before the transaction, but the Target will only contain the value that are being updated.

    hth,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by howexg9 Monday, October 28, 2013 4:18 PM
    Friday, October 25, 2013 9:33 PM
    Answerer
  • Hi,

    The best way to find the fields those are updated through plugin is Target property.

    Let me explain the difference between Pre Image, Post Image and Target.

    - Pre image contains all the values before update.

    - Post Image will always contain updated values.

    - Target will only contain fields those are updated. Suppose user has updated Name field on particular entity then you will only get Name property in Target. Suppose user has updated Name and Dirtyfield then in target you will get both the fields.

    You can check if Attribute was updated or not using the below code.

    if(targetEntity.Contains("Name")) // It will return true if the name was updated

    {

    }

    HTH!

    Thanks!

    • Marked as answer by howexg9 Monday, October 28, 2013 4:18 PM
    Saturday, October 26, 2013 5:57 AM

All replies

  • Hi,

    The target entity will only contain the values that are changing, the in your example using bool as data type will mean you will get false even if the value is not being updated. You really should use bool? (this is what is generated in the early bound types which I can't recommend enough!). If a value is not being updated in the current transaction, there will be no attribute so the following would return false:

    var isUpdated = updateEntity.Contains("dirtyfield");

    You have a number of choices to finding if a value is changing:

    1. Use the fact that the value is contained within the Target entity as an indication that it is changing - although, it could be an update with the value that is the same as the current record.

    2. Compare the PreImage to the target entity if there is a value in the target entity

    3. Compare the PreImage value to the PostImage value - this will tell you if the value has changed.

    I personally use option 2.

    hth,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Friday, October 25, 2013 8:06 PM
    Answerer
  • Thanks Scott for your reply. My code just a simple example. It doesn't mean it has to be bool. I just use it as an example. Base on your suggestion, here is my another question. I'm just about putting preimage in my plugin. I thought it should be able to capture all previous values in updateEntity, then I can compare the difference between updateEntity and postEntity. That's why I didn't use preimage. Am I understanding right?

    Thanks.

    Friday, October 25, 2013 8:26 PM
  • Hi,

    The PreImage is not the same as the Target. The PreImage will contain the values you request in the registration before the transaction, but the Target will only contain the value that are being updated.

    hth,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by howexg9 Monday, October 28, 2013 4:18 PM
    Friday, October 25, 2013 9:33 PM
    Answerer
  • Hi,

    The best way to find the fields those are updated through plugin is Target property.

    Let me explain the difference between Pre Image, Post Image and Target.

    - Pre image contains all the values before update.

    - Post Image will always contain updated values.

    - Target will only contain fields those are updated. Suppose user has updated Name field on particular entity then you will only get Name property in Target. Suppose user has updated Name and Dirtyfield then in target you will get both the fields.

    You can check if Attribute was updated or not using the below code.

    if(targetEntity.Contains("Name")) // It will return true if the name was updated

    {

    }

    HTH!

    Thanks!

    • Marked as answer by howexg9 Monday, October 28, 2013 4:18 PM
    Saturday, October 26, 2013 5:57 AM