通过C#插件在Dynamic CRM中自动填充查找字段。

huangapple go评论71阅读模式
英文:

Auto populate lookup field through plugin in C# plugin in Dynamic CRM

问题

我正在尝试在选择联系人查找字段时自动填充我的表单中的帐户查找字段,但当我执行时,它显示实体不包含相应的 ID,但当我调试时,它显示正确的帐户名称,有人可以告诉我我在哪里犯了错误吗?

Entity task = (Entity)context.InputParameters["Target"];
EntityReference lookupValue = task.GetAttributeValue<EntityReference>("new_contact");

if (lookupValue != null)
{
    string targetEntityName = lookupValue.LogicalName;
    Guid targetEntityId = lookupValue.Id;
    QueryExpression query_contactparty = new QueryExpression("contact");
    query_contactparty.Criteria = new FilterExpression();
    query_contactparty.ColumnSet.AddColumns("contactid");
    query_contactparty.ColumnSet.AddColumns("fullname");
    query_contactparty.ColumnSet.AddColumns("parentcustomerid");
    query_contactparty.Criteria.AddCondition("contactid", ConditionOperator.Equal, targetEntityId);

    LinkEntity accountLink = new LinkEntity("contact", "account", "parentcustomerid", "accountid", JoinOperator.Inner);
    accountLink.Columns = new ColumnSet("name");
    query_contactparty.LinkEntities.Add(accountLink);

    EntityCollection contactparty = service.RetrieveMultiple(query_contactparty);

    foreach (var con in contactparty.Entities)
    {
        Guid accountid = con.GetAttributeValue<Guid>("contactid");
        string name = con.GetAttributeValue<string>("fullname");
        EntityReference parentAccount = con.GetAttributeValue<EntityReference>("parentcustomerid");

        if (parentAccount != null)
        {
            //throw new InvalidPluginExecutionException("Value found is:" + name + " and id is: " + accountid + " account :" + parentAccount.Name);

            task["new_account"] = new EntityReference("new_kaispe", parentAccount.Id)
            {
                Name = parentAccount.Name
            };
        }

        service.Update(task);
    }
}
英文:

I am trying to auto populate account lookup field in my form upon selection of contact lookup field but when i perform it shows entity does not contain the respective id but when i debug it shows the right account name can any one let me know where i am making mistake?

Entity task = (Entity)context.InputParameters[&quot;Target&quot;];
EntityReference lookupValue = task.GetAttributeValue&lt;EntityReference&gt;(&quot;new_contact&quot;);

if (lookupValue != null)
{
    string targetEntityName = lookupValue.LogicalName;
    Guid targetEntityId = lookupValue.Id;
    QueryExpression query_contactparty = new QueryExpression(&quot;contact&quot;);
    query_contactparty.Criteria = new FilterExpression();
    query_contactparty.ColumnSet.AddColumns(&quot;contactid&quot;);
    query_contactparty.ColumnSet.AddColumns(&quot;fullname&quot;);
    query_contactparty.ColumnSet.AddColumns(&quot;parentcustomerid&quot;);
    query_contactparty.Criteria.AddCondition(&quot;contactid&quot;, ConditionOperator.Equal, targetEntityId);

    LinkEntity accountLink = new LinkEntity(&quot;contact&quot;, &quot;account&quot;, &quot;parentcustomerid&quot;, &quot;accountid&quot;, JoinOperator.Inner);
    accountLink.Columns = new ColumnSet(&quot;name&quot;);
    query_contactparty.LinkEntities.Add(accountLink);

    EntityCollection contactparty = service.RetrieveMultiple(query_contactparty);

    foreach (var con in contactparty.Entities)
    {
        Guid accountid = con.GetAttributeValue&lt;Guid&gt;(&quot;contactid&quot;);
        string name = con.GetAttributeValue&lt;string&gt;(&quot;fullname&quot;);
        EntityReference parentAccount = con.GetAttributeValue&lt;EntityReference&gt;(&quot;parentcustomerid&quot;);

        if (parentAccount != null)
        {
            //throw new InvalidPluginExecutionException(&quot;Value found is:&quot; + name + &quot; and id is: &quot; + accountid + &quot; account :&quot; + parentAccount.Name);

            task[&quot;new_account&quot;] = new EntityReference(&quot;new_kaispe&quot;, parentAccount.Id)
            {
                Name = parentAccount.Name
            };
        }

        service.Update(task);
    }
}

答案1

得分: 1

在处理“Update”消息的插件管道中,可以在预验证阶段设置“EntityReference”值。只需要修改“InputParameters”集合的“Target”属性中找到的“Entity”,不要尝试使用“service.Update(task)”来更新它。

只需删除那行代码并将您的插件注册到预验证阶段。

英文:

In a plugin pipeline handling the Update message, an EntityReference value can be set in the pre validation stage. The Entity found in the "Target" property of the InputParameters collection only needs to be modified. Do not try to update it using service.Update(task).

Just remove that line and register your plugin for the pre validation stage.

huangapple
  • 本文由 发表于 2023年2月6日 18:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定