web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

verifying an empty field with Post-Image on plugin.

(0) ShareShare
ReportReport
Posted on by

afternoon,

I have a plugin that needs to verify the contents of 4 fields before sending a SOAP request (for formatting purposes).  these are  "a_gis_sitename", "a_gis_street", "a_gis_town", "a_gis_postcode".  While I appreciate that i can format these client-side, I would prefer to save myself an extra field in my form (it is already a substantial form.)

I have created a post-image with the alias "postImage" of the fields in question and written the following:

string[] siteFields = { "sitename", "street", "town", "postcode" };
string siteDetails = "";
int i = 0;
int isEmpty = 0; // if isEmpty hits 3, the GIS fields haven't been populated.
// now the image of the case needs to be checked to see if fields were empty.
if (context.PostEntityImages.Contains("postImage") && context.PostEntityImages["postImage"] is Entity) {
var postImage = (Entity)context.PostEntityImages["postImage"];
foreach (string f in siteFields) {
// add each field to comma separated list making sure first item in list isn't preceded by comma.
if (postImage.Attributes["a_gis_" + f] != null)
siteDetails += ((i++ > 1) ? ", " : "") + (string)postImage.Attributes["a_gis_" + f];
else isEmpty++;
}
if (isEmpty < 3) siteDetails = "Location of problem: " + siteDetails + ". ";
}

stack trace persistently returns a 

The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at myService.Plugins.dogFouling.Execute(IServiceProvider serviceProvider)

Why is this happening.  CRM can be such a headache to debug.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    a viable solution is to replace  -- if (postImage.Attributes["a_gis_" + f] != null) -- with -- if (postImage.Attributes.Contains("a_gis_" + f)) --  

    However!!

    why use a post image at all in that case? couldn't you make a similar call against the data the plugin would hold already?

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

    myEntity.Contains("a_gis_fieldName")...

    So here's a further question:

    I can see where Pre-image is useful but why use a post image at all if these kind of checks can easily be made without it?!

    (as an aside, the i++ above resolves incorrectly and should be ++i)

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Jonni,

    Pre image is useful to get the previous value data before the changes, and you need the post image to get the updated value data.

    For example,

    You want to prevent user change the email address if the email address already contains correct data, with @mycompany.com with the @yahoo.com, so you need to compare the previous value, so that you can prevent the user to update the valid email address with the public email address.

    Post image will be useful and valid only for post update.

    Anyway, for your case:

    // now the image of the case needs to be checked to see if fields were empty.

    You should use PostImage if you want to check under Post event.

    You can use preimage, if you combine preimage with target entity.

  • Community Member Profile Picture
    on at

    Thanks for your reply, please see my second post.  I needed to do a compare rather than a blind reference to an early bound field that hadn't been recorded by the plugin.

    I understand pre-image and I can see why post-image is useful for post-update. I'm just mystified as to why the latter would be available for create tasks. It muddies the water. In the create context, it seems to perform the same task as context.InputParameters["Target"];

    Maybe I'm missing the point. Unfortunately, I find MSDN opaque in its explanation. I learn more from stack overflow than MS material.

  • Mohammed Fakhri Profile Picture
    402 on at

    Hi Aileen,

    How do i know in plugin if user has changed lookup value to null. ?

    Do i use targetEntity or a Pre Post Image?

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Jonniosaurus,

    The error you have specified "The given key was not present in the dictionary" occurred when an entity does not contains value for given field name.

    Instead of using entity.Attributes["attributeName"];

    You can use entity.GetAttributeValue<TypeOfField>("attributeName");

    Example,

    entity.GetAttributeValue<string>("a_gis_street");

    It will return null if entity doesn't have value for "a_gis_street", instead of throwing an Exception.

    Hope this helps,

    Please mark as Answered if you got it.

    Thanks & Regards,

    Himanshu Prajapati

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans