locked
Plugin Key has a null value RRS feed

  • Question

  • As always I apologize if this has been answered before, please feel free to point me in another thread. I have looked, but haven't seen a solution to my issues yet.

    I have a plugin I wrote that is triggered on an Update to the contact entity.

    It is a post update with a postimage so that I can grab the updated Field data.

    There are three fields in here, and all three of them could end up with a null value.

    The plugin works great if the data is there, but if the value has been updated to NULL, I get the "the given key was not present in the dictionary" error.

    ---

    I could probably do this on a pre-update as well with a pre image... but I feel like I would end up with the same problem.
    I have to use images, because the data isn't always updated, but I need to reference those values.

    I use them to create another record on a custom entity.

    So I guess my real question, and something I'm not very familiar with is...

    How does one set up the plugin so that if one of the fields is null, it doesn't try the rest of the code and I can avoid this error?


    Daniel

    Thursday, December 12, 2013 1:54 AM

Answers

  • Hi Daniel,

    You can ensure like as below that attribute contains data or not :

     if (PostImage.Attributes.Contains("new_fieldname"))
    {
          // do your stuff here
    }



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog


    • Edited by _Vikram Thursday, December 12, 2013 2:13 AM
    • Marked as answer by I am Oop Thursday, December 12, 2013 2:19 AM
    Thursday, December 12, 2013 2:13 AM
  • Daniel, 

    That depends on your business requirement if all fields are required to achieve your requirement, you can add && conditions on the same if statement. 

    if ((PostImage.Attributes.Contains("new_fieldname1")) && (PostImage.Attributes.Contains("new_fieldname2")))
    {
          // do your stuff here
    }

    Otherwise you could use nested if statement.



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    • Marked as answer by I am Oop Thursday, December 12, 2013 2:19 PM
    Thursday, December 12, 2013 2:27 AM

All replies

  • oh, to make it clear. If any of those values are null, I want to exit out of the code and have it do nothing.

    one of them is an EntityReference the other two are OptionValues


    Daniel

    Thursday, December 12, 2013 2:03 AM
  • Hi Daniel,

    You can ensure like as below that attribute contains data or not :

     if (PostImage.Attributes.Contains("new_fieldname"))
    {
          // do your stuff here
    }



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog


    • Edited by _Vikram Thursday, December 12, 2013 2:13 AM
    • Marked as answer by I am Oop Thursday, December 12, 2013 2:19 AM
    Thursday, December 12, 2013 2:13 AM
  • Vikram,

    That helps a lot.. thank you....

    This does, however lead into another question (that is relevant to this).

    I need it to fail on any of three field names... How would I end up having it fail on any of the three. This method, while great, wouldn't necessarily handle three different field names. Is there an "or" statement (I've looked and not been able to find)

    Or should I use a Nested IF statement?


    Daniel


    • Edited by I am Oop Thursday, December 12, 2013 2:20 AM
    Thursday, December 12, 2013 2:19 AM
  • Daniel, 

    That depends on your business requirement if all fields are required to achieve your requirement, you can add && conditions on the same if statement. 

    if ((PostImage.Attributes.Contains("new_fieldname1")) && (PostImage.Attributes.Contains("new_fieldname2")))
    {
          // do your stuff here
    }

    Otherwise you could use nested if statement.



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    • Marked as answer by I am Oop Thursday, December 12, 2013 2:19 PM
    Thursday, December 12, 2013 2:27 AM
  • that has been incredibly helpful Vikram, very much appreciate the help.

    Have a great day.


    Daniel

    Thursday, December 12, 2013 2:20 PM
  • Vikram,


    You've been very helpful. But I do have another related question. I hope I'm not being a complete pain.

    what if I wanted to fire off code if the PostImage doesn't contain a value for the field.

    this works great for making sure it's there
    (if ((PostImage.Attributes.Contains("new_fieldname1")) )

    But what if want to know if it's not there? (or the value in question has become null?)

    I tried doing something like this:

    Created the following:
                string parentaccount;
                parentaccount = null;

    Then did the following to populate it with a value IF it exists in the code

                if (postImageEntity.Attributes.Contains("parentcustomerid"))
                { parentaccount = ((OptionSetValue)(postImageEntity.Attributes["parentcustomerid"])).Value.ToString(); }

    but when I try to reference that data later on it seems to always have the null value that it got set with originally. I appreciate your patience with me, this is all pretty new to me, and sometimes when trying to look things up, I don't have the proper terminology to be able to find what I need. 



    Daniel

    Thursday, December 12, 2013 7:11 PM
  • Vikram,

    okay.. I think I got this figure out. But wanted to run it by you and see if there is a hole I don't understand.

    I changed the code to this:

    string parentaccount;

                if (postImageEntity.Attributes.Contains("parentcustomerid"))
                { parentaccount = ((OptionSetValue)(postImageEntity.Attributes["parentcustomerid"])).Value.ToString(); }

    else parentaccount = null;

    And it looks like it is working correctly. Do you see anything wrong with this?


    Daniel

    Thursday, December 12, 2013 7:30 PM
  • Well.. that seemed to work well for the OptionSetValue...

    but what if the issue is a lookup field?

    So here is the situation now.

    I have a lookup field on the Contact record that points to an Account record.

    I've tried using Post Image and Pre Image, but it seems that they both end up with Null values when this record gets updated.

    I need to close some related entities when the Account is removed from the Contact record.

    But I can't figure out how to have an action take place if the EntityReference is null.

                if (postImageEntity.Attributes.Contains("parentcustomerid"))
                { parentaccount = ((EntityReference)(postImageEntity.Attributes["parentcustomerid"])).Value.ToString(); }

    else parentaccount = null;

    This code does not work. Any help, or point me in the direction of a way to deal with a Null EntityReference field would be awesome.


    Daniel

    Friday, December 13, 2013 9:03 AM