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 :
Microsoft Dynamics CRM (Archived)

I need to retrieve a related entity in a plugin going through a many to one relationship

(0) ShareShare
ReportReport
Posted on by

I am writing a custom workflow / plugin to be fired for a custom entity when a record that is of type custom_entity is deactivated.  Custom_entity is related to Account and the relationship is many to one (or looking at it from Account's perspective, Account has a one to many relationship with Custom_entity).

When a record of type Custom_entity is deactivated, I need to check to see if the owning Account can be deactivated.  First step in that process is to lay hands on the owning Account, which would you think would be simple.

Apparently it's not.  It appears to take herculean effort to accomplish that one minimal thing.

Here is what I have tried:

relatedAccountReference = custom_Entity.GetAttributeValue<EntityReference>("new_Account");
relatedAccount = service.Retrieve("account", relatedAccountReference.Id, columnsAccount);
relatedAccountId = relatedAccountReference.Id;

The plugin is failing on the first line with error: Object reference not set to an instance of an object.

Earlier in the code I'm using the org service to retrieve custom_Entity.  Since it's GUID is available through the context, retrieving this record is straight forward.  The column set it uses does not contain "new_Account".  If I try to add new_Account as a column in the columnset, I get a different error: The given key is not present in the dictionary.

I'm about to pull my hair out.  All I need to do is access the related account that an instance of custom_Entity is bound to.  The lookup field in CRM for this linkage is called "new_Account".

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nuno Profile Picture
    2,005 on at

    Hi,

    To get the related account:

    EntityReference accountId = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("new_Account"))

                           .GetAttributeValue<EntityReference>("new_Account");

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    How are you getting the custom_Entity attributes?

    Are you using target, or are your retrieving the custom entity attributes?

    I think you might need to retrieve the custom_Entity, and only then you will be able to get the EntityReference of the new_Account field:

    Basically:

    Entity custom = service.Retrieve("custom_Entity", customEntityId, new ColumnSet(true)(;

    EntityReference accountRef = custom.GetAttributeValue<EntityReference>("new_Account");

    Entity account = service.Retrieve("account", accountRef.Id, columnsAccount);

    // Your additional logic here...

  • bpoindexter Profile Picture
    on at

    Error: 'custom_Entity' entity doesn't contain attribute with Name = 'new_Account'.

    Of course, this doesn't make sense.  It absolutely does contain an attribute called new_Account.  I can see it in field list for custom_Entity in the solution.

  • bpoindexter Profile Picture
    on at

    Aric,

    I adjusted my code to match your suggestions.  custom_Entity is being retrieved like this:

    custom_Entity_Id = context.PrimaryEntityId;   <- the custom_Entity is the context of the workflow; workflow fires when custom_Entity is activated or deactivated.


    custom_Entity = service.Retrieve("custom_Entity",custom_Entity_Id, new columnSet(true));

    Then what follows is my code as posted in my original post.  Reposted here:

    relatedAccountReference = custom_Entity.GetAttributeValue<EntityReference>("new_Account");
    relatedAccount = service.Retrieve("account", relatedAccountReference.Id, columnsAccount);
    relatedAccountId = relatedAccount.Id

    Error now reads: 'custom_Entity' doesn't contain attribute with Name = 'new_Account'.

    I did change all my column sets to include all columns for all entities being retrieved.

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     you may need to use lowercase new_account..

  • bpoindexter Profile Picture
    on at

    Thanks to everyone for the input, I did get it working.  I was not using correct case to match the Logical name of the entity.  Banged my head against the wall for a whole on that one.  Thanks everyone for the input, answers will be marked.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans