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

Crm Plugin : How to store retrieved column into string variable

(0) ShareShare
ReportReport
Posted on by 87

Hi All,

I am facing some difficulty while developing a plugin to update a value. 

I am fetching value of pricelist from respective opportunity entity on creation of opportunity product.

I am using another query to find the discount details of pricelist using the value of pricelist retrieved from previous query. I have written the following code:

string parentOpportunity = ((EntityReference)e.Attributes["opportunityid"]).Name;

QueryExpression query = new QueryExpression("opportunity");
ColumnSet columns = new ColumnSet("pricelevelid", "name");
query.ColumnSet = columns;
query.Criteria.AddCondition("name", ConditionOperator.Equal, parentOpportunity);
EntityCollection pricelistItems = Service.RetrieveMultiple(query);
var a = pricelistItems.Entities[0];

string priceList1 = a.Attributes["pricelevelid"].ToString();

QueryExpression query2 = new QueryExpression("pricelevel");
ColumnSet columns2 = new ColumnSet("new_basicdealerdiscount");
query2.ColumnSet = columns2;
query2.Criteria.AddCondition("pricelevelid", ConditionOperator.Equal, priceList1);
EntityCollection discountItem = Service.RetrieveMultiple(query2);

But when I run this code it is giving me error:

An exception System.FormatException was thrown while trying to convert input value 'Microsoft.Xrm.Sdk.EntityReference' to attribute 'pricelevel.pricelevelid'. Expected type of attribute value: System.Guid...........

Please help.

Thanks in advance !!

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

    Hello,

    Try to use following code:

    string parentOpportunity = ((EntityReference)e.Attributes["opportunityid"]).Name;
    
    QueryExpression query = new QueryExpression("opportunity");
    ColumnSet columns = new ColumnSet("pricelevelid", "name");
    query.ColumnSet = columns;
    query.Criteria.AddCondition("name", ConditionOperator.Equal, parentOpportunity);
    EntityCollection pricelistItems = Service.RetrieveMultiple(query);
    var a = pricelistItems.Entities[0];
    
    var priceList1 = a.GetAttributeValue("pricelevelid");
    
    QueryExpression query2 = new QueryExpression("pricelevel");
    ColumnSet columns2 = new ColumnSet("new_basicdealerdiscount");
    query2.ColumnSet = columns2;
    query2.Criteria.AddCondition("pricelevelid", ConditionOperator.Equal, priceList1.Id);
    EntityCollection discountItem = Service.RetrieveMultiple(query2);

  • Amrita P Profile Picture
    87 on at

    Hi Andrew,

    You are awesome. Thank you so much it worked.

    Thanks

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
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans