Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Entity Reference to String

Posted on by Microsoft Employee

Hello,
I am new to CRM C# development. I have the following piece of code:
internal static Entity CreateContractLine(Prod productstar, decimal quantity, Guid userId, Guid contractId, IOrganizationService crmService, Guid parentContractLine, bool isRelated = false)
{

Entity crmdetail = new Entity(Constants.EntityName.ContractLine); //Constants is the name of a common file and it is referring to the logical name of an entity called ContractLine.

crmdetail[Constants.Attributes.ContractLine.ProductId] = new EntityReference(Constants.EntityName.Product, productstar.id); //left part is the schema name of an attribute called ProductId.



the ProductId attribute in the entity crmdetail is a single line of text field in CRM. I need to check if this attribute contains the word "Cheese Pizza" as part of its string. How do I do that?

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Entity Reference to String

    Thank you so much ! That helped!

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Entity Reference to String

    Then you need to query it from the server.

    Might do it this way:

    var product = service.Retrieve(Constants.EntityName.Product, productstar.id, new ColumnSet("name"));//Make sure it's actually called "name"on the product entity

    var productName = (string)product["name"];

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Entity Reference to String

    Hi Alex,

    You're right. It is setting to an entity reference. I need to read this attribute value and check as part of its string contains the phrase "Cheese Pizza"

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Entity Reference to String

    Hi,

     in your code, you seem to be setting that attribute, not reading from it.. Yet you are setting it to an entity reference.. Could you clarify what/where you need to do?

     As a side not.. when you have an entity reference, sometimes you will have "Name" in the entity reference. Sometimes, it won't be there (depends how that entity reference ended up there). If it's not there, you need to retrieve that entity from CRM (service.Retrieve), include "name" attribute into the list of columns, and, then, you'll have the name

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans