英文:
When use Update message for dataverse plugin, I have the error "Exception Message: The given key was not present in the dictionary."
问题
在代码的"stage 1"和"stage 2"之间(第4行),有一个错误:
> 异常消息:给定的键在字典中不存在。
这段代码用于检查表中的重复记录,在"Create"消息中运行正常,但在"Update"消息中运行时出现上述错误。我已经将代码复制在这里:
if (entity.LogicalName == "atmtransaction")
{
tracingService.Trace("Stage {0}", "1");
OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
tracingService.Trace("Stage {0}", "2");
string store = (string)entity["store"];
tracingService.Trace("Stage {0}", "3");
DateTime date = (DateTime)entity["date"];
tracingService.Trace("Stage {0}", "4");
if (stagingType.Value != 1)
{
tracingService.Trace("Stage {0}", "5");
// 检查是否已存在具有相同存储和日期的记录
QueryExpression query = new QueryExpression("ada_atmtransaction");
query.Criteria.AddCondition("ada_store", ConditionOperator.Equal, store);
query.Criteria.AddCondition("ada_date", ConditionOperator.Equal, date);
EntityCollection results = service.RetrieveMultiple(query);
if (results.Entities.Count > 0)
{
tracingService.Trace("Stage {0}", "6");
throw new InvalidPluginExecutionException("已存在具有相同存储和日期的记录。");
}
}
}
对此有任何想法吗?
英文:
I have a dataverse plugin C# code like below, in the trace "stage 1" between "stage 2" (line 4),
OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
there is a error:
> Exception Message: The given key was not present in the dictionary.
The code is executed to check the duplicate in a table, it works in Create message, but I create a new step, but it did not work in Update message, the error is above, I have copied the code here
if (entity.LogicalName == "atmtransaction")
{
tracingService.Trace("Stage {0}", "1");
OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
tracingService.Trace("Stage {0}", "2);
string store = (string)entity["store"];
tracingService.Trace("Stage {0}", "3");
DateTime date = (DateTime)entity["date"];
tracingService.Trace("Stage {0}", "4");
if (stagingType.Value != 1)
{
tracingService.Trace("Stage {0}", "5");
// Check if a record already exists with the same store and date
QueryExpression query = new QueryExpression("ada_atmtransaction");
query.Criteria.AddCondition("ada_store", ConditionOperator.Equal, store);
query.Criteria.AddCondition("ada_date", ConditionOperator.Equal, date);
EntityCollection results = service.RetrieveMultiple(query);
if (results.Entities.Count > 0)
{
tracingService.Trace("Stage {0}", "6");
throw new InvalidPluginExecutionException("A record already exists with the same store and date.");
}
}
}
Anyone idea about this?
答案1
得分: 1
你在下面这行代码中遇到了这个错误:
OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
原因:在更新操作中,很可能字段 "stagingtype" 没有被改变或更新,因此你在实体对象中找不到这个字段,我怀疑这个实体对象是目标对象。你需要显式地获取包含 "stagingtype" 字段的记录,然后再进行检查。
另外,我建议你查看 "preImage" 和 "postImage",这对你也会有帮助。
详细信息可以参考以下链接:
https://learn.microsoft.com/en-us/power-apps/developer/data-platform/understand-the-data-context#entity-images
英文:
You get this error on below line
OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
Reason: On update most probably field stagingtype is not changed/updated and hence you will not find this field in entity object, which I suspect is the Target.
You will have to explicitly fetch the record with stagingtype field and then check.
In addition I would sugest look into preImage and postImage this will be helpful for you as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论