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

我有一个权限“帐户”,它在Microsoft Dynamics CRM中有一些name_field。 除了查找字段之外,还可以插入每个其他字段值。 如何在查找中选择现有值?

我使用以下代码为查找字段添加值..但是我没有收到任何错误..

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 

我如何更改代码以在查找字段中选择“主要”值?

CRM 2011中的查找字段是EntityReference ,这意味着您需要知道查找指向的实体的LogicalName和记录的Id

所以你的代码将是:

 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 

一个考虑因素:你写道

 Account acc = new Account(); 

我不知道你是使用早期绑定(意味着crmsvcutil.exe生成的类)还是后期绑定(在这种情况下你将编写Entity acc = new Entity("account");

但如果你使用早期绑定,语法将是这样的:

 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 

使用早期绑定,您将知道该字段所期望的正确类型。

根据您所描述的内容,帐户类型是一个实体(假设它被称为new_accounttype),带有名称字段(new_name),并且有三个实例名为“Primary”,“Secondary”和“Other”。 Lookupfieldid是链接到new_accounttype表的外键。 这意味着,为了将帐户类型查找设置为“主要”,您需要知道帐户类型实例的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); 

获取查找字段的值

 EntityReference entref = (EntityReference)item.Attributes[attributeName]; var LookupId = entref.Id; var logicalName = entref.LogicalName; 

设置查找字段的值

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