Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a value to a lookup field?

I have a entitiy "account "in which it has some name_field in Microsoft Dynamics CRM. Other than lookup field , every other fields values can be inserted. how to select an existing value in look up????

I used the following code to add value to the lookup field.. However I don't get any error..

Account acc = new Account();
acc.Attributes["name"] = "Ram"; // this values got inserted
acc.Attributes["age"] = "22"; // this values got inserted
acc.Attributes["lookupfieldid"] = "Sampletext";

service.Create(acc); // to create account

How I need to change my code in order to select "primary" value in the lookup field?

like image 564
Ram K Avatar asked Apr 10 '14 07:04

Ram K


People also ask

How do I edit a lookup field?

Update the properties of a lookup fieldOpen a table in Design View. Click the lookup field's name in the Field Name column. Under Field Properties, click the Lookup tab. Set the Display Control property to Combo Box to see all available properties changes to reflect your choice.


2 Answers

Lookup fields in CRM 2011 are EntityReference, this means you need to know the LogicalName of the entity the lookup is pointing and the Id of the record.

so your code wil be:

Account acc = new Account();
acc.Attributes["name"] = "Ram"; // this values got inserted
acc.Attributes["age"] = "22"; // this values got inserted

acc.Attributes["lookupfieldid"] = new EntityReference("contact", contactId); // if lookupfieldid is pointing to contact entity

service.Create(acc); // to create account

One consideration: you wrote

Account acc = new Account();

I don't know if you are using early bound (means the classes generated by crmsvcutil.exe) or late bound (in this case you will write Entity acc = new Entity("account");)

but if you use early bound, the syntax will be something like:

Account acc = new Account();
acc.Name = "Ram"; // this values got inserted
acc.Age = "22"; // this values got inserted
acc.LookupFieldId = new EntityReference("contact", contactId); // if lookupfieldid is pointing to contact entity
service.Create(acc); // to create account

using early bound you will know before the right type expected by the field.

like image 92
Guido Preite Avatar answered Oct 12 '22 13:10

Guido Preite


Based on what you have described, account type is an entity (lets assume it is called new_accounttype) with a name field (new_name) and there are three instances named "Primary", "Secondary" and "Other." Lookupfieldid is a foreign key that links to the new_accounttype table. This means that in order to set the account type lookup to "Primary" you need to know the guid of the account type instance where new_name = "Primary".

//Retrieve "Primary" account type
QueryExpression query = new QueryExpression("new_accounttype");
query.Criteria.AddCondition("new_name", ConditionOperator.Equal, "Primary");
Entity accountType = service.RetrieveMultiple(query).Entities.First();

//Set the lookup as Guido described above
Account acc = new Account();
acc.Attributes["name"] = "Ram";
acc.Attributes["age"] = "22";
acc.Attributes["lookupfieldid"] = new EntityReference("new_accounttype", accountType.Id);
service.Create(acc);
like image 35
Zach Mast Avatar answered Oct 12 '22 12:10

Zach Mast