Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

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

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

  • 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
    Bipin D365 28,964 Super User 2024 Season 1 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
    a33ik 84,323 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
    Daniel Schneider 170 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans