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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

field is avaliable but it is showing Key Not found exception

(0) ShareShare
ReportReport
Posted on by

Hi,

when I am trying to retrieve some data from opportunity through plugin it is showing Key not found exception. I created the plugin for opportunity entity only. can anyone please suggest how to resolve this issue. Thanks in Advance.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Its good practice when you are getting field value from context entity , you need to check with contains like below. Please validate your plugin you are doing the same -

    Entity entity = (Entity)context.InputParameters["Target"];
    if (entity.Attributes.Contains("FieldName"))
    {
     //Retrieve the value
      if (entity.GetAttributeValue<string>("FieldName") != "" && entity.GetAttributeValue<string>("FieldName") != null)
      {
        string someVal = entity.GetAttributeValue<string>("FieldName");
      }
    }


  • Suggested answer
    Piotr Kerner Profile Picture
    285 on at

    Hi,

    In the plugin you will only get the fields that ectually changed, so unless you change your field on form (or other places where the plugin is called) the field won't be present in the Target.

    You can use the PreImage (register it under the plugin step) to always have the value avaliable in your plugin. You can then do something like this:

                var target = (pluginExecutionContext.InputParameters["Target"] as Entity);
                var image = (pluginExecutionContext.PreEntityImages["PreImage"] as Entity);

                var yourValue = target.Contains("new_yourvalue") ? target["new_yourvalue"] : image["new_yourvalue"];

  • Suggested answer
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Hi Indrasena,

    There are a few common reasons of this issue:

    Reason 1: Please make sure you are mentioning the field name in Column Set in order to retrieve its value from CRM. For Example: 

    QueryExpression contactquery = new QueryExpression
    {
    EntityName="contact",
    ColumnSet = new ColumnSet("firstname", "lastname", "contactid")
    };

    Here, I have mentioned only fields ("firstname", "lastname", "contactid") not the emailaddress but if f try to retrieve the value of emailaddress value, it will give Key Not found error.

    Same thing applicable, when you are retrieving the data using service.Retrieve.

    Reason 2: Trying to retrieve the unchanged field's value from the context. For Example, If my plugin is running on Update of Contact entity. I will get only updated/changed field values in COntext. If I will try to get the unchanged value I will get Key not found error.

    Reason 3: Providing wrong attribute name while preparing entity object to create/Update the record in CRM. Especially in case of Late Binding, Provide the logical name of an attribute in lowercase (All chars should be in lower case).

    Reason 4: Always check for Null or Empty value, while retrieving it's data:

    For Example:

    Entity accountentity = service.Retrieve("account", accountId, new ColumnSet(true));

    if (accountentity.Attributes.Contains("new_account_id")&& accountentity.Attributes["new_account_id"]!=null)

    {

    }  

    Hope you get the root cause.

    If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'. It would be helpful to the other community members seeking to resolve a similar issue.


    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.in

     

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    You can also enable tracing to see the trace logs by which you will get the attribute name where its exactly failed .Make sure you have written trace log in every field retrieval step in your plugins.

    Please have a look below article how to enable trace a and check log  in your plugin .

    www.powerobjects.com/.../debugging-your-plug-ins-with-the-plug-in-trace-log

    Hope this helps.

  • Community Member Profile Picture
    on at

    I understood Arpit.  

    QueryExpression query = new QueryExpression("lead");

                           query.ColumnSet.AddColumns("fullname","companyname", "address1_country", "address1_city", "address1_postalcode");

                           query.Criteria = new FilterExpression();

                           query.Criteria.AddCondition("qualifyingopportunityid", ConditionOperator.Equal, entity.Id);

                           tracingService.Trace(entity.Id.ToString());

                           EntityCollection results = _service.RetrieveMultiple(query);

    from this query I am getting all the records except address1_postalcode. I tried with console application also. I am getting same issue.

    I crosschecked schema name and everything.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Indrasena,

    Do you have any data in that field? If there is no data then even if you have added the field in the columns it woould be null if no data.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi @Ravi.  Data is Available  and it's the default field in CRM

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Sorry Indrasena but did you check of all the records has address1_postalcode populated?

    Do you get any records If you run an advanced find query to select accounts where address1_postalcode doesn't contain data

  • Suggested answer
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Please make sure, you are doing NULL/EMPTY check before retrieving the field's value.

    if(entityObj.Attributes.Contains("address1_postalcode"))

    {

    string postalcode = entityObj["address1_postalcode"].ToString();

    }

    Hope it helps.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans