英文:
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["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);
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论