devzero
devzero

Reputation: 2680

Unable to save GUID to microsoft crm entity

I have the service declared as:

   protected OrganizationService Service = 
        new OrganizationService(
            new CrmConnection(
                new ConnectionStringSettings(

I then update a guid field like this:

        var query = new QueryExpression("contact");
        query.ColumnSet.AddColumn("contactid");
        query.ColumnSet.AddColumn("parentcustomerid");
        query.Criteria.AddCondition("contactid", ConditionOperator.Like, crmPerson.PersonId);
        var result = Service.RetrieveMultiple(query);
        if (result.Entities.Any())
        {
            var r = result.Entities[0];
            r.Attributes["parentcustomerid"] = crmPerson.OrganizationId;
            Service.Update(r);
        }

I get the following error when I do this (string fields seems to work)

System.ServiceModel.FaultException`1 was unhandled
  HResult=-2146233087
  Message=System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #52B2CA82
  Source=mscorlib

How do I fix this?

Upvotes: 0

Views: 748

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

parentcustomerid is a lookup field, you need to set the value using an EntityReference, not a Guid.

r.Attributes["parentcustomerid"] = new EntityReference("account", accountId);

Upvotes: 4

Related Questions