Hi every one I'm getting an error
System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
string posttopic = string.Empty;
if (context.PostEntityImages.Contains("PrimaryContactImage") && context.PostEntityImages["PrimaryContactImage"] is Entity)
{
Entity postMessageImage = (Entity)context.PostEntityImages["PrimaryContactImage"];
posttopic = postMessageImage.GetAttributeValue<string>("codec_primaryprofessionalgroup").ToString();
}
entity.Attributes.Add("codec_professionalgroups", posttopic);
}
}
}
}
Any idea why?
Found the solution
the answer was to set the string using the below line of code
posttopic = ((EntityReference)postMessageImage.Attributes["primaryprofessionalgroup"]).Name;
think it did not work otherwise because primaryprofessionalgroup is a look up value and not a string
Hi,
Use the below code. Ensure that
-- Your plugin step is registered on PreOperation
-- Your plugin step has a postimage with the name "PrimaryContactImage"
-- The schema names of your field "codec_primaryprofessionalgroup" & "codec_professionalgroups" are correct.
-- The fields "codec_primaryprofessionalgroup" & "codec_professionalgroups" are both lookup to a same entity.
==============
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (context.PostEntityImages.Contains("PrimaryContactImage") && context.PostEntityImages["PrimaryContactImage"] is Entity)
{
Entity postMessageImage = context.PostEntityImages["PrimaryContactImage"];
entity["codec_professionalgroups"] = postMessageImage["codec_primaryprofessionalgroup"];
}
}
}
================
Hope this helps.
What is the field type of codec_professionalgroups?
If it's lookup then you need to set the value below way instead of entity.Attributes.Add("codec_professionalgroups", posttopic);
So replace the last line with this-
entity.Attributes["entity"] = new EntityReference("codec_professionalgroups", topic.Id);
Hi Das, I tried using the code you provided but im still getting the same error , and yes it is a look up type
Hi Jonathan ,
What is the field type of codec_primaryprofessionalgroup ? Seems this is a lookup field , you need to retrieve the lookup value by using EntityReference . Once you get the entity reference you can get the id, name and logical name.
public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity)context.InputParameters["Target"]; string posttopic = string.Empty; if (context.PostEntityImages.Contains("PrimaryContactImage") && context.PostEntityImages["PrimaryContactImage"] is Entity) { Entity postMessageImage = (Entity)context.PostEntityImages["PrimaryContactImage"]; // posttopic = postMessageImage.GetAttributeValue<string>("codec_primaryprofessionalgroup").ToString();
if (postMessageImage.Attributes.Contains("codec_primaryprofessionalgroup")) { EntityReference topic = (EntityReference)postMessageImage.Attributes["codec_primaryprofessionalgroup"]; posttopic = topic.Name.ToString(); // topic.Id - Retrieve the GUID // topic.LogicalName --Retrieve the entity name } } entity.Attributes.Add("codec_professionalgroups", posttopic); } }