web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.

(0) ShareShare
ReportReport
Posted on by 165

Hello,

Below is part of my code. I know why I am getting this error, what I need is a workaround or a solution to this problem.
Thank you.

if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
    
                Entity Case = (Entity)context.InputParameters["Target"];


                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    string serialNumber = "";
                    if (Case.GetAttributeValue("zst_serialnumber") != null)
                    {
             
                        serialNumber = Case.GetAttributeValue("zst_serialnumber").Id.ToString();
                        //serial number is a lookup field
                    }
                    
                    Entity Warranty = new Entity("zst_warranty");

                    QueryExpression query = new QueryExpression("zst_warranty");
                    query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
                    query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);
                    //zst_serialno is a lookup field

                    EntityCollection collection = service.RetrieveMultiple(query);

                    if (collection.Entities.Count > 0)
                    {
                        Case["zst_activewarranty"] = true;
                    }
                    else if (collection.Entities.Count == 0)
                    {
                        Case["zst_activewarranty"] = false;
                    }

                    service.Update(Case);
                }

I have the same question (0)
  • Suggested answer
    Daniel Schneider Profile Picture
    172 on at

    Hi,

    i think the problem is because you try to compare string with Lookup Field which is a EntityReference too. Try

    var serialNumber = Case.GetAttributeValue<EntityReference>("zst_serialnumber").Id;

    QueryExpression query = new QueryExpression("zst_warranty");

    query.ColumnSet = new ColumnSet(new string[] { "zst_name" });

    query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);

    Hope this help you or is what you mean.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Replace line

    if (Case.GetAttributeValue<string>("zst_serialnumber") != null)

    with line

    if (Case.GetAttributeValue<EntityReference>("zst_serialnumber") != null)

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Use below updated code and then check.

    if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                    {
    
                        Entity Case = (Entity)context.InputParameters["Target"];
    
    
                        IOrganizationServiceFactory serviceFactory =
                            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                        try
                        {
                            Guid serialNumber = Guid.Empty;
                            if (Case.GetAttributeValue("zst_serialnumber") != null)
                            {
    
                                serialNumber = Case.GetAttributeValue("zst_serialnumber").Id;
                                //serial number is a lookup field
                            }
    
                            if (serialNumber != Guid.Empty)
                            {
                                Entity Warranty = new Entity("zst_warranty");
    
                                QueryExpression query = new QueryExpression("zst_warranty");
                                query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
                                query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);
                                //zst_serialno is a lookup field
    
                                EntityCollection collection = service.RetrieveMultiple(query);
    
                                if (collection.Entities.Count > 0)
                                {
                                    Case["zst_activewarranty"] = true;
                                }
                                else if (collection.Entities.Count == 0)
                                {
                                    Case["zst_activewarranty"] = false;
                                }
    
    
                                service.Update(Case);
                            }
                        }

    Lookup field condition value should be GUID not string.

    query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);

    You should use EntityReference not string in If codnition.

    Case.GetAttributeValue<EntityReference>("zst_serialnumber") != null

    Please mark my answer verified if i were helpful

  • Disu Ridwan Ayodeji Profile Picture
    5 on at

    Hi Kumar,

    Trust all is fine..

    Am having same error log while debugging my plug-in.

    What am I doing wrong.

    Here is the code below.

    //The InputParameters collection contains all the data passed in the message request.

               if (context.InputParameters.Contains("Target") &&

               context.InputParameters["Target"] is Entity)

               {

                   //Obtain the target entity from the input parameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   if (entity.LogicalName != "incident")

                       return;

                   IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                   try

                   {

                       //Plug-in business logic goes here.

                           if (entity.Contains("isw_issuesubcategory") && context.MessageName == "Create")

                               if (entity.Attributes.Contains("isw_issuesubcategory"))

                               {

                                   var value = entity.GetAttributeValue<string>("isw_issuesubcategory");

                                   if (value != "Credit Card Management")

                                       return;

                                   incidentid = entity.Id;

                                   Entity incident = service.Retrieve("incident", incidentid,

                                   new ColumnSet("isw_issuesubcategory", "ticketnumber",

                                   "title", "customerid", "isw_issuecategory",

                                   "isw_issue", "isw_issueproduct", "caseorigincode", "isw_conflictofinteres",

                                   "isw_supportteam", "isw_kblink ", "isw_servicequeue"));

                                   string NgTicketnumber = incident.Attributes["ticketnumber"].ToString();

                                   string NgTitle = incident.Attributes["title"].ToString();

                                   string NgCustomerId = incident.Attributes["customerid"].ToString();

                                   string NgIssuecategory = incident.Attributes["isw_issuecategory"].ToString();

                                   string NgIssue = incident.Attributes["isw_issue"].ToString();

                                   string NgIssueProduct = incident.Attributes["isw_issueproduct"].ToString();

                                   string NgCaseOriginCode = incident.Attributes["caseorigincode"].ToString();

                                   string NgConflictOfInterest = incident.Attributes["isw_conflictofinteres"].ToString();

                                   string NgSupportTeam = incident.Attributes["isw_supportteam"].ToString();

                                   string NgKbLink = incident.Attributes["isw_kblink"].ToString();

                                   string NgServiceQueue = incident.Attributes["isw_servicequeue"].ToString();

                                   string test = "Title";

                                   //Call webservice here

                                   entity.Attributes["isw_disputeplatform"] = "worked";

                                   service.Update(entity);

    Thank you

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 157

#2
ManoVerse Profile Picture

ManoVerse 153 Super User 2026 Season 1

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans