crm update prevalidation插件无法获取实体

时间:2013-08-09 07:36:17

标签: c# plugins dynamics-crm-2011 dynamics-crm crm

我有一个crm 2011的插件。使用PreImage进行更新时的预验证。我可以使用上下文获得PreImage。通过做:

var preImage = context.PreImage.ToEntity<EntityName>();

这为我提供了preImage,因此没有更改的值。现在,我尝试使用上下文来获取具有更改值的实体。

var update = context.GetEntity.ToEntity<EntityName();

但是我没有得到任何数据。

如何获取更新的数据?任何人都可以帮助我,告诉我为什么这不起作用。我希望这是足够的代码。

1 个答案:

答案 0 :(得分:2)

我希望以下代码能为您提供帮助。

protected void ExecuteYourPlugin(LocalPluginContext localContext)
{
    IPluginExecutionContext pluginContext = localContext.PluginExecutionContext;
    // Get Target Entity
    var updateEntity = (Entity)pluginContext.InputParameters["Target"];

    // Get PreImage Entity
    var preEntity = (Entity)localContext.PluginExecutionContext.PreEntityImages["PreImage"];

    // Is in Update
    if(localContext.PluginExecutionContext.MessageName.ToLower() == "update")
    {
        var fieldName = string.Empty;

            // If Contains, Extract value from Target Entity
            if(updateEntity.Contains("new_fieldname"))
            {
                // Cast the fields according their type
                fieldName = updateEntity["new_fieldname"].toString();
            }
            // Else extract the value from preImage
            else if(preEntity .Contains("new_fieldname"))
            {
                // Cast the fields according their type
                fieldName = preEntity["new_fieldname"].toString();
            }
    }
}