MMoore94
MMoore94

Reputation: 54

Update CRM Lookup Field from C#

I am attempting to develop an application to allow updates to our CRM database via a gridview.

Everything I have was working till attempting to update a lookup field.

The following is working C# code:

_service = GlobalFunctions.GetServiceAccount();
GlobalFunctions.User u = (GlobalFunctions.User)Session["UserInfo"];

Entity aspiration = new Entity("tog_aspiration");
aspiration.Id = new Guid(inputGridView.DataKeys[e.RowIndex].Value.ToString());
aspiration["tog_nofollrl"] = Int32.Parse(tog_nofollrl);
_service.Update(aspiration);

Now when attempting to add the lookup field to the update it gives me an error.

_service = GlobalFunctions.GetServiceAccount();
GlobalFunctions.User u = (GlobalFunctions.User)Session["UserInfo"];

Entity aspiration = new Entity("tog_aspiration");
aspiration.Id = new Guid(inputGridView.DataKeys[e.RowIndex].Value.ToString());
aspiration["tog_nofollrl"] = Int32.Parse(tog_nofollrl);
aspiration["tog_techidname"] = new EntityReference("equipment", new Guid(ddlVet.SelectedValue));
_service.Update(aspiration);

Here is the error

Incorrect attribute value type System.String 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Incorrect attribute value type System.String

Upvotes: 0

Views: 2379

Answers (1)

Jordi
Jordi

Reputation: 1470

Just to make sure, is the lookup field called tog_techidname or just tog_techid? Every lookup has an associated name field, which is the lookup name + "name", but you can't update it directly. It is enough to just update the id with the EntityReference.

Try with

aspiration["tog_techid"] = new EntityReference("equipment", new Guid(ddlVet.SelectedValue));

Upvotes: 1

Related Questions