Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Entity Reference Not Working As Expected in Plugin

Posted on by 25

A relationship for the CRM Product entity has been created in the code I am working with. 

Later in the code, we get:

    invoiceItem["client_name"] = product.Name;

I know this works.

I am trying to do the following, immediately after that code:

  invoiceItem["client_extendeddescription"] = product.GetAttributeValue<string>("client_extendeddescription");

Visual Studio Reports: "EntityReference does not contain definition GetAttributeValue and accessible extension method 'GetAttributeValue' could not be found"

If from the product entity I can get the name why can I not get a field value? I appreciate the help.  I am new to the C#/Plugin development side so I hope this is not a stupid question. 

Thanks,

Steve 

*This post is locked for comments

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Entity Reference Not Working As Expected in Plugin

    This line cannot throw that error message as we are not doing any entity operation here. Is it possible to debug and see where exactly it is failing.

  • Steve Kessler Profile Picture
    Steve Kessler 25 on at
    RE: Entity Reference Not Working As Expected in Plugin

    Okay, I think the issue is coming from later in the code. Here is what I just added.

    Screen-Shot-2019_2D00_07_2D00_31-at-8.58.01-PM.png

    Here is the line I think is the issue but it was working before I made the change above. 

    invoiceItem["client_productid"] = new EntityReference("dmcc_product", billingproduct.GetAttributeValue<EntityReference>("client_productid").Id);

    Thanks,

    Steve 

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Entity Reference Not Working As Expected in Plugin

    Hi Steve,

    Something is not correct there. Can you share a screenshot with the error message? This error message meaanas that we are trying to retrieve an entity record and have passed "Client_product" as the entity name but this name is not correct.

  • Steve Kessler Profile Picture
    Steve Kessler 25 on at
    RE: Entity Reference Not Working As Expected in Plugin

    Ravi,

    Thank you for this great response. This makes perfect sense.  I think I am almost there.

    I added:

    var productEnt = service.Retrieve(product.LogicalName, product.Id, new ColumnSet("client_extendeddescription"));

    invoiceItem["client_extendeddescription"] = productEnt.GetAttributeValue<string>("client_extendeddescription");

    The error message I get back when running the code is that "The entity with a name = 'client_product' with namemapping = 'Logical' was not found in the MetadataCache. "

    This does not make sense because we are looking at the core product entity rather than a custom entity called client_product.

    Thanks,

    Steve

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Entity Reference Not Working As Expected in Plugin

    Hi,

    Entity reference doesn't contains the attributes. It only contains the entity logical name, entity guid and in some cases name of the record.

    In order to get the fields, you first need to retrieve the Entity object and then you can use GetAttributeValue.

    Refer below code to get the entity from the entity reference-

    var productEnt = service.Retrieve(product.LogicalName, product.Id, new ColumnSet(true));

    Hope this helps.

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans