locked
CRM 2011 Plug in (SetState question) RRS feed

  • Question

  • Hello. First, I have to apologize if this question has been asked in the past. I've been digging and digging for about a week, and I haven't found what I've been looking for. I may not be searching correctly for what I need, as this is all fairly new to me.

    What I'm wondering is if it is even possible to do what I am trying to do, or if it's better to do this with a workflow. (Which it currently is).

    I've been tasked with learning C# and Plugins at the company and I thought I'd start out with something that I thought would be relatively straight forward, but doesn't seem to be.

    So, here goes.
    What I would like to do to is update a number of fields within the Contact entity when the contact is deactivated.

    I've been able to create plugins on the Update entity that will edit these fields. But when I try to create a plugin off the SetState or SetStateDynamicEntity, I get invalid key errors when trying to update records within the contact that just got deactivated.

    I've tried this in Post Operation and Pre Operation, but no matter what I do, I can't seem to get it to be able to update a field within the Contact Entity that was deactivated.

    I'm not actually asking for any specifics on how to do this, but I'm starting to wonder if it's even possible.

    Summary.

    Contact Alfred is deactivated. So I want the following fields cleared (set to NULL)

    Employer, Web Login.

    Now I know the field names (and have no problems manipulating the data with updates based on something other than Status), but they don't seem to exist when creating a plugin on SetState or SetStateDynamicEntity (or, I just don't know how to make them appear?)

    any push in the right direction would be helpful. Even if it's just a link to something on here that already covered this.. or telling me that it's not possible and to move in a different direction.

    Thank you in advance.


    Daniel

    Tuesday, November 5, 2013 9:51 PM

Answers

  • Hi Daniel What you are trying will be possible in pre plugin step for SetState or SetSateDynamic. The.fields you Re trying to fetch are not present int he context as they are not changed. If all you were trying to do is update the contact values, this cannot be done in post event as the record is deactivated and is read-only in the system. To perform anything on post event, you would need to activate the record in the plugin code first. The behaviour is similar to what you see on UI so you should be able to take inspiration from there. Also if you already have workflow for the same, I see no reason why you need plugin for something like this. You should be able to continue using workflow for the same. -Minal
    • Marked as answer by I am Oop Wednesday, November 6, 2013 12:01 AM
    Tuesday, November 5, 2013 10:50 PM
  • Hi Daniel,

         The context only contains fields that has changed. So if the field "fieldname" is not changed, it will not be in the context and you can get key not found error. To avoid this you use the image. You can use PreImage to get the value of any field before the operation. However if there is field level security and the user cannot see the value, plug-in will not be able to fetch it either. Here is more in terms of images.

    http://msdn.microsoft.com/en-us/library/gg309673.aspx

    https://community.dynamics.com/crm/b/crminogic/archive/2010/07/26/pre-image-38-post-image-explained-33.aspx

    http://sumedha8.blogspot.com.au/2012/10/sample-plug-in-compare-pre-and-post.html


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     

    • Marked as answer by I am Oop Wednesday, November 6, 2013 9:24 PM
    Wednesday, November 6, 2013 9:08 PM

All replies

  • Hi Daniel What you are trying will be possible in pre plugin step for SetState or SetSateDynamic. The.fields you Re trying to fetch are not present int he context as they are not changed. If all you were trying to do is update the contact values, this cannot be done in post event as the record is deactivated and is read-only in the system. To perform anything on post event, you would need to activate the record in the plugin code first. The behaviour is similar to what you see on UI so you should be able to take inspiration from there. Also if you already have workflow for the same, I see no reason why you need plugin for something like this. You should be able to continue using workflow for the same. -Minal
    • Marked as answer by I am Oop Wednesday, November 6, 2013 12:01 AM
    Tuesday, November 5, 2013 10:50 PM
  • Minal...

    Thank you very much for the detailed answer. It answers a lot of questions that were on my mind about how the different states work.  This will be very helpful moving forward.

    Most likely I will keep this as a workflow, but I thought it would be a simple one to play around with and get my feet wet, and help me learn more about plugins and C#. It has done that, but there are things that I am very unaware of as well, and this highlighted my lack of knowledge.

    All of what you say makes complete sense, and I hope that this question helps others at some point in the future.

    Have a great night.


    Daniel

    Wednesday, November 6, 2013 12:01 AM
  • again, thank you for the information. As I said, this is mostly for learning how plugins work...

    But you say that I should be able to do this on Pre plugin.... I've tried it with both Prevalidate and pre Operation and I'm still getting the key was not present in the dictionary.

    Here is what I've tried to do to.... The first one is what I use with an update transaction.

                Entity record = (Entity)context.InputParameters["Target"];

    And that allows me to reference the record and set the value with the following:

      record["fieldname"] = null;

    But with the SetState or SetStateDynamic that doesn't work. And I've tried several different variations, including:

    EntityReference record= (EntityReference)context.InputParameters["EntityMoniker"];

    Again, this will probably never go live, but I'm just trying to figure out how things all work together, and I think this may be a key to my understanding. Again.. thank you so much for your help so far.


    Daniel


    • Edited by I am Oop Wednesday, November 6, 2013 3:26 PM
    Wednesday, November 6, 2013 3:26 PM
  • Hi Daniel,

         The context only contains fields that has changed. So if the field "fieldname" is not changed, it will not be in the context and you can get key not found error. To avoid this you use the image. You can use PreImage to get the value of any field before the operation. However if there is field level security and the user cannot see the value, plug-in will not be able to fetch it either. Here is more in terms of images.

    http://msdn.microsoft.com/en-us/library/gg309673.aspx

    https://community.dynamics.com/crm/b/crminogic/archive/2010/07/26/pre-image-38-post-image-explained-33.aspx

    http://sumedha8.blogspot.com.au/2012/10/sample-plug-in-compare-pre-and-post.html


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     

    • Marked as answer by I am Oop Wednesday, November 6, 2013 9:24 PM
    Wednesday, November 6, 2013 9:08 PM
  • Minal,


    That is good stuff. Thank you so much for taking the time to help me understand this. I very much appreciate it.



    Daniel

    Wednesday, November 6, 2013 9:24 PM