Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Query expression using customerid

(0) ShareShare
ReportReport
Posted on by 2

I need to write a query expression using customerid but the below query is throwing format exception. Please help

QueryExpression query = new QueryExpression("opportunity");
Guid contactid = new Guid("23E62DCE-F154-E843-814K-005056BB4062");
query.Criteria.AddCondition("customerid",ConditionOperator.Equal,new EntityReference("contact",contactid));
EntityCollection result = _orgService.RetrieveMultiple(query);

*This post is locked for comments

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at
    RE: Query expression using customerid

    Hi Lathin,

    You dont need to pass the Entity reference in the above code,

    Please try with below code,

    QueryExpression query = new QueryExpression("opportunity");

    Guid contactid = new Guid("23E62DCE-F154-E843-814K-005056BB4062");

    query.Criteria.AddCondition("customerid",ConditionOperator.Equal,contactid);

    EntityCollection result = _orgService.RetrieveMultiple(query);

    Best Regards,

    Shahbaaz

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: Query expression using customerid

    You just need to use like below

    query.Criteria.AddCondition("customerid",ConditionOperator.Equal,contactid);

  • ashlega Profile Picture
    34,477 on at
    RE: Query expression using customerid

    I don't think you need to pass EntityReference - just pass the Guid to your condition

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Query expression using customerid

    Hi ,

    Try with this , replace column name which you want to retrieve.

    Guid contactid = new Guid("23E62DCE-F154-E843-814K-005056BB4062");

    ConditionExpression condition1 = new ConditionExpression();

    condition1.AttributeName = "customerid";

    condition1.Operator = ConditionOperator.Equal;

    condition1.Values.Add(contactid);            

    FilterExpression filter1 = new FilterExpression();

    filter1.Conditions.Add(condition1);

    QueryExpression query = new QueryExpression("opportunity");

    query.ColumnSet.AddColumns("column1", "column2"); // Replace column name .

    query.Criteria.AddFilter(filter1);

    EntityCollection result1 = _orgService.RetrieveMultiple(query);

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans