When use Update message for dataverse plugin, I have the error "Exception Message: The given key was not present in the dictionary."

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

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"消息中运行时出现上述错误。我已经将代码复制在这里:

  1. if (entity.LogicalName == "atmtransaction")
  2. {
  3. tracingService.Trace("Stage {0}", "1");
  4. OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
  5. tracingService.Trace("Stage {0}", "2");
  6. string store = (string)entity["store"];
  7. tracingService.Trace("Stage {0}", "3");
  8. DateTime date = (DateTime)entity["date"];
  9. tracingService.Trace("Stage {0}", "4");
  10. if (stagingType.Value != 1)
  11. {
  12. tracingService.Trace("Stage {0}", "5");
  13. // 检查是否已存在具有相同存储和日期的记录
  14. QueryExpression query = new QueryExpression("ada_atmtransaction");
  15. query.Criteria.AddCondition("ada_store", ConditionOperator.Equal, store);
  16. query.Criteria.AddCondition("ada_date", ConditionOperator.Equal, date);
  17. EntityCollection results = service.RetrieveMultiple(query);
  18. if (results.Entities.Count > 0)
  19. {
  20. tracingService.Trace("Stage {0}", "6");
  21. throw new InvalidPluginExecutionException("已存在具有相同存储和日期的记录。");
  22. }
  23. }
  24. }

对此有任何想法吗?

英文:

I have a dataverse plugin C# code like below, in the trace "stage 1" between "stage 2" (line 4),

  1. 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

  1. if (entity.LogicalName == "atmtransaction")
  2. {
  3. tracingService.Trace("Stage {0}", "1");
  4. OptionSetValue stagingType = (OptionSetValue)entity["stagingtype"];
  5. tracingService.Trace("Stage {0}", "2);
  6. string store = (string)entity["store"];
  7. tracingService.Trace("Stage {0}", "3");
  8. DateTime date = (DateTime)entity["date"];
  9. tracingService.Trace("Stage {0}", "4");
  10. if (stagingType.Value != 1)
  11. {
  12. tracingService.Trace("Stage {0}", "5");
  13. // Check if a record already exists with the same store and date
  14. QueryExpression query = new QueryExpression("ada_atmtransaction");
  15. query.Criteria.AddCondition("ada_store", ConditionOperator.Equal, store);
  16. query.Criteria.AddCondition("ada_date", ConditionOperator.Equal, date);
  17. EntityCollection results = service.RetrieveMultiple(query);
  18. if (results.Entities.Count > 0)
  19. {
  20. tracingService.Trace("Stage {0}", "6");
  21. throw new InvalidPluginExecutionException("A record already exists with the same store and date.");
  22. }
  23. }
  24. }

Anyone idea about this?

答案1

得分: 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

  1. 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.

https://learn.microsoft.com/en-us/power-apps/developer/data-platform/understand-the-data-context#entity-images

huangapple
  • 本文由 发表于 2023年7月10日 20:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653722.html
匿名

发表评论

匿名网友

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

确定