简体   繁体   中英

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?

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.

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);

Get Value of Lookup fields

EntityReference entref = (EntityReference)item.Attributes[attributeName];

var LookupId = entref.Id;

var logicalName = entref.LogicalName;

Set Value of Lookup fields

newAccount[attributeName] = new EntityReference(logicalName, LookupId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM