Why is CRM 2011 Entity Relationship null in this plugin code?

自古美人都是妖i 提交于 2019-12-01 21:00:16

The entity contained in the InputParameters["Target"] only includes the changed fields that were submitted in the update, not all fields. Your plugin works for Create because InputParameters["Target"] always contains all fields during a Create obviously.

To fix this, you need to add a PreImage to your step in PluginRegistrationTool that includes the primarycontactid field. A PreImage will always include the values for the fields you specify as they were before the update.

The trick is to check for primarycontactid on InputParameters["Target"] first, because that would contain the latest value (if the user updated both new_receivesnewsletter and primarycontactid during the same save, for example). If InputParameters["Target"] doesn't contain primarycontactid, then fall back on your PreImage, accessed something like this (assuming you set your PreImage's alias to "preimage"):

context.PreEntityImages["preimage"].GetAttributeValue<EntityReference>("primarycontactid")

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!