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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 107 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 94

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans