Search code examples
c#dynamics-crm

CRM Error When Assigning Lookup Value Incorrect type of attribute value System.Int32


On CRM 2011 I'm writing a C# to create new Accounts by reading from a .csv spreadsheet. The Account entity has a lookup field "Salesperson" that is a lookup value against the CRM "SystemUser" entity. The "Salesperson" field is a fixed value, so I'm just assigning the SystemUser Guid into it. However I got an error "Incorrect type of attribute value System.Int32" when trying to assign this lookup value using the SystemUser Guid. What would be the correct way to assign a Guid value?

Thanks in advance for your help.

Uri organizationUri = new Uri("https://mycrm.org.com/crmtest/XRMServices/2011/Organization.svc");
            var cred = new ClientCredentials();
            OrganizationServiceProxy _serviceproxy = new OrganizationServiceProxy(organizationUri, null, cred, null);
            _service = (IOrganizationService)_serviceproxy;
Program p = new Program();

Entity account = new Entity("account");
account.Attributes["salesperson"] = "69ACA8F0-78B9-C211-B753-99E3B511A6F7";
Guid g2 =   p._service.Create(account);

Solution

  • I'm not where or how you are getting the Incorrect type of attribute value System.Int32 I have a thought that this is subset of the code, i.e., that is Program p = new Program(); doing and how does it get assigned a _service?

    Per the CRM SDK there is no field on CRM 2011's Account entity called salesperson (http://msdn.microsoft.com/en-us/library/gg328057.aspx). I'm assuming this is a custom field that you added to the entity and the name should look like xxxx_salesperson. Look at the customization of the entity to find the schema name of the field.

    Next, you can't assign a Guid directly to a lookup field. You need to assign an EntityReference so it should read:

    account.Attributes["salesperson"] = new EntityReference("systemuser", Guid.Parse("69ACA8F0-78B9-C211-B753-99E3B511A6F7"));