简体   繁体   English

如何将值添加到查找字段?

[英]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. 我有一个权限“帐户”,它在Microsoft Dynamics CRM中有一些name_field。 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. CRM 2011中的查找字段是EntityReference ,这意味着您需要知道查找指向的实体的LogicalName和记录的Id

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"); ) 我不知道你是使用早期绑定(意味着crmsvcutil.exe生成的类)还是后期绑定(在这种情况下你将编写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." 根据您所描述的内容,帐户类型是一个实体(假设它被称为new_accounttype),带有名称字段(new_name),并且有三个实例名为“Primary”,“Secondary”和“Other”。 Lookupfieldid is a foreign key that links to the new_accounttype table. Lookupfieldid是链接到new_accounttype表的外键。 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". 这意味着,为了将帐户类型查找设置为“主要”,您需要知道帐户类型实例的guid,其中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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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