【问题标题】:How to add a value to a lookup field?如何向查找字段添加值?
【发布时间】:2014-04-10 07:13:44
【问题描述】:

我有一个实体“帐户”,其中在 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

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

【问题讨论】:

    标签: c# dynamics-crm-2011 dynamics-crm


    【解决方案1】:

    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
    

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

    【讨论】:

    • 感谢重播,我的要求是在查找字段中插入默认值“Primary”。它有主要,次要和其他。我需要在创建帐户时插入主要。如何将值“主要”添加到我的查找字段中。??
    • 我是新手..我到处搜索,但找不到。 :(请帮帮我
    • 至少张贴一张截图以了解您要更新的确切字段
    • 我的要求是从本地 sql 数据库中读取姓名、年龄、帐户类型(查找字段)等内容到 Dynamic CRM 2011..
    • postimg.org/image/9mne4hgcj screenshot added。如果我的数据库内容显示帐户类型是“主要”,我想将相同的内容上传到 CRM。如何选择查找字段中的值?
    【解决方案2】:

    根据您所描述的,帐户类型是一个实体(假设它称为 new_accounttype),具有名称字段 (new_name),并且有三个实例,分别名为“主要”、“次要”和“其他”。 Lookupfieldid 是链接到 new_accounttype 表的外键。这意味着要将帐户类型查找设置为“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);
    

    【讨论】:

    • 感谢您的回复..我尝试了您的代码。它显示以下异常:- 尝试将输入值“Primary”转换为属性“new_accounttype.new_name”时抛出异常 System.FormatException。预期的属性值类型:System.Guid。引发异常:Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。
    • new_name 是您的 new_accounttype 实体上的字符串字段吗?
    【解决方案3】:

    获取查找字段的值

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

    设置查找字段的值

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2021-03-10
      • 2020-01-24
      • 2021-12-09
      • 2021-12-20
      • 2023-03-31
      相关资源
      最近更新 更多