Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert EntityReference to Entity

Does anyone know how can Convert EntityReference to Entity.

protected override void Execute(CodeActivityContext executionContext)
{
    [Input("Email")]
    [ReferenceTarget("email")]
    public InArgument<Entity> EMail { get; set; }


    Entity MyEmail = EMail.Get<Entity>(executionContext);

This give me an error. Cannot convert this.

like image 659
hello B Avatar asked Mar 07 '13 17:03

hello B


People also ask

How do I get an EntityReference entity?

You do that like this. IOrganizationService organization = ...; EntityReference reference = ...; Entity entity = organization. Retrieve(reference. LogicalName, reference.Id, new ColumnSet("field_1", "field_2", ..., "field_z"));

What is EntityReference in plugin?

EntityReference(String, Guid) Initializes a new instance of the EntityReference class setting the logical name and entity ID. This constructor was introduced with Microsoft Dynamics CRM Online 2015 Update 1 and cannot be used with earlier versions.


1 Answers

The shortest answer to your questions is to query the database for the entity that's pointed out (referred to) by the entity reference. I've always viewed entity references as (rough) equivalent to the pointers in C++. It's got the address to it (guid) but you need to de-reference it in order to get to the honey. You do that like this.

IOrganizationService organization = ...;
EntityReference reference = ...;

Entity entity = organization.Retrieve(reference.LogicalName, reference.Id, 
  new ColumnSet("field_1", "field_2", ..., "field_z"));

When I do a lot of converting from EntityReference to Entity, I deploy the extension method with optional parameter for the fields.

public static Entity ActualEntity(this EntityReference reference,
  IOrganizationService organization, String[] fields = null)
{
  if (fields == null)
    return organization.Retrieve(reference.LogicalName, reference.Id, 
      new ColumnSet(true));
  return organization.Retrieve(reference.LogicalName, reference.Id, 
    new ColumnSet(fields));
}

You can read more and compare EntityReference and Entity.

like image 113
Konrad Viltersten Avatar answered Sep 22 '22 06:09

Konrad Viltersten