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)

Not able to retrieve all attributes of a entity in Query Expression

(0) ShareShare
ReportReport
Posted on by 437

Hi All, 

I am writing a service in which I need to get some attribute of a Opportunity entity.but 

I m not getting some attribute . Dont know why can you guys pls help me in this .

static void Main(string[] args)
{
var crmService = CRMHelperClass.GetCRMService();
QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
// qe.ColumnSet.Columns.Add("XXX_estdateoffirstinvoice");
 qe.ColumnSet.AllColumns = true;
//qe.ColumnSet.Columns.Add("new_calculate");


// Condition where task attribute equals account id.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "statecode";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(0);
qe.Criteria.AddCondition(condition);

EntityCollection ec = crmService.RetrieveMultiple(qe);

foreach (var r in ec.Entities)
{

     if( convertTodecimal(r.attribute["XXX_estdateoffirstinvoice"])   > 0 ) // Giving error that key not present

};

}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    BadrinathB Profile Picture
    970 on at

    What is the datatype of your attribute?

    Check if the attribute present or not in your entity?

    Use Entity.GetAttributeValue<T> instead of  if( convertTodecimal(r.attribute["XXX_estdateoffirstinvoice"])   > 0 )

    If that attribute is not present in the Entity instance, it will not generate an error.  Instead, the method will return a “default value” of Type T, or null if T is nullable.

    Read the below post for more detailed explanations

    community.dynamics.com/.../entity-getattributevalue-lt-t-gt-explained

    community.dynamics.com/.../getattributevalue-demystified

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Hi

    When the attribute value is blank it would not retrieve those attributes.

    So you have to check whether the attribute is present for that record before getting the value for the same.

    r.Attributes.Contains("XXX_estdateoffirstinvoice")

  • Suggested answer
    Community Member Profile Picture
    on at

    use this simple line:

    var value = r.getAttributeValue(decimal)("XXX_estdateoffirstinvoice");

    if (value > 0)

    then do whatever....

    save you the time to check if entity got this attribute then do this...

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    The query expression will give key not found error when the value of the field is null.

    So you should first check if the attribute contains value using the expression below.

    foreach (var r in ec.Entities)
    {
       if(r.Attributes.Contains("XXX_estdateoffirstinvoice"))
    { if(convertTodecimal(r.Attributes["XXX_estdateoffirstinvoice"]) > 0 )
    { //Your code here } } };

    Hope this helps.

  • windyMill Profile Picture
    437 on at

    Thanks Gopalan.

    The issue was that most of the records had blanked attributes.

  • windyMill Profile Picture
    437 on at

    Can you please  tell me how I can update some attributes value while going through the loop

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