Skip to main content

Notifications

Announcements

No record found.

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

  • Disu Ridwan Ayodeji Profile Picture
    5 on at
    RE: unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.

    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

  • Verified answer
    Bipin D365 Profile Picture
    28,979 Moderator on at
    RE: unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.

    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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.

    Hello,

    Replace line

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

    with line

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

  • Suggested answer
    Daniel Schneider Profile Picture
    172 on at
    RE: unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.

    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.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,336 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans