Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'

(0) ShareShare
ReportReport
Posted on by

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?

  • Verified answer
    Community Member Profile Picture
    on at
    RE: getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'

    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

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.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.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'

    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);

  • Community Member Profile Picture
    on at
    RE: getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'

    Hi Das, I  tried using the code you provided but im still getting the same error , and yes it is a look up type

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: getting error System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'

    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); } }

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans