How to add a value to a lookup field?
Solution 1
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.
Solution 2
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);
Related videos on Youtube
Ram K
Updated on July 04, 2022Comments
-
Ram K almost 2 years
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?
-
Ram K about 10 yearsThanks for the replay,, My requirement is to insert default value "Primary" into look up field. It has primary,secondary and other.I need to insert primary while creating account.How can i add value "Primary" into my lookup fields.??
-
Ram K about 10 yearsI'm newbie.. i searched completely everywhere ,but couldnt find it. :( Help me please
-
Guido Preite about 10 yearsat least post a screenshot to understand which exact field you want to update
-
Ram K about 10 yearsMy requirement is to read the contents like name,age,account type(look up field)etc from local sql database into Dynamic CRM 2011..
-
Ram K about 10 yearspostimg.org/image/9mne4hgcj screenshot added.if the my db contents says account type is "primary" i want to upload the same content to CRM. How can select the values in lookup field?
-
Guido Preite about 10 years"Type of Account" is a custom field? is a lookup or an optionset(combobox)?
-
Guido Preite about 10 yearsif it is a lookup you need to use the code I already provided, you need to get the logicalname of entity and the id you want to set.
-
Ram K about 10 yearsThanks for your reply ..I tried your code.It shows the following exception :- An exception System.FormatException was thrown while trying to convert input value 'Primary' to attribute 'new_accounttype.new_name'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).