To get lookup details through Plugin in Dynamics Crm

We did a plugin to get lookup reference and set the data in current form. Plugin on post operation,
Update method.

        // Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
try
{
// Refer to the lead entity .
if (entity.LogicalName == "lead")
{
if (entity.Attributes.Contains("parentcontactid"))

//to get data of lookup we used entityrefernce
var currency = ((EntityReference)(entity.Attributes["parentcontactid"]));
var actualCurrency = service.Retrieve(currency.LogicalName, currency.Id, new ColumnSet(true));
var currencyName = actualCurrency["firstname"].ToString();
var last = actualCurrency["lastname"].ToString();
//throw new InvalidPluginExecutionException("The contact123" + last);
entity.Attributes["firstname"] = currencyName;
//throw new InvalidPluginExecutionException("The contact" + entity["firstname"]);
entity.Attributes["lastname"] = last;
}
}
// Obtain the organization service reference.
service.Update(entity);
}
catch (FaultException<OrganizationServiceFault> ex)
    {
throw new InvalidPluginExecutionException("Howdy! error occurred" +ex.Message);
}
}

No comments:

Post a Comment