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 :
Customer experience | Sales, Customer Insights,...
Answered

How to check for Null condition while retrieving data using plugin

(0) ShareShare
ReportReport
Posted on by 135

Hello All,

I want to check null condition before i retrieve the value 

Entity simEntity = service.Retrieve("sim", simid,new Microsoft.Xrm.Sdk.Query.ColumnSet("ordernumber", "number", "cps"));

String number = simEntity.Attributes["number"].ToString();

when the value is null in sim entity i get plugin error as System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. 

I tried using the below format but no luck:

Entity simEntity = service.Retrieve("sim", simid,new Microsoft.Xrm.Sdk.Query.ColumnSet("number"));

if (simEntity .Attributes.Contains("number") && entity["number"] != null){

String number = simEntity.Attributes["number"].ToString();

}

Please suggest.

Regards,

Hima

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello Hima,

    Everything depends on how you plan to use the value from the field.

    Here is one way you can do it:

    if (simEntity.Contains("number") && simEntity["number"] != null)

    {

    var number = simEntity["number"].ToString();

    }

    alternative way is following but you have to define what type you expect:

    var number = simEntity.GetAttributeValue<string>("number");

    if simEntity will not contain "number" field this GetAttributeValue will return null.

  • HimaM Profile Picture
    135 on at

    Hello Andrew,

    Thanks you so much for the reply.

    When I'm retrieving fields from the other entity using

    Entity simEntity = service.Retrieve("sim", simid,new Microsoft.Xrm.Sdk.Query.ColumnSet("number"));

    if the number field is null its doesn't return the null value and proceed instead we get an error message as given key not found. How do i fetch the field value even if its null? any idea?

    Regards,

    Hima

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hima,

    That's a platform feature. If the field has null value it is not returned so you should use one of approaches I recommended in my reply.

    Good luck.

  • HimaM Profile Picture
    135 on at

    Thank you! it’s working now.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans