我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复355或者20190827可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!

前面的文章 Dynamics 365 CE在Pre Delete插件中应用Image 讲了Delete消息PreOperation阶段Image的应用,今天我们来看下Update 消息PostOperation阶段Image的尝试。

我使用如下代码简单注册一个插件:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.ServiceModel;
using System.Text; namespace CRM.Plugins
{
public class PostWorkOrderUpdate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity currentEntity = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity preImgEntity;
Entity postImgEntity;
Entity retrieveEntity;
StringBuilder sb = new StringBuilder();
try
{
if (context.PreEntityImages.Contains("PreImg"))
{
preImgEntity = context.PreEntityImages["PreImg"];
}
else
{
throw new InvalidPluginExecutionException("Pre Update image - PreImg does not exist!");
}
if (context.PostEntityImages.Contains("PostImg"))
{
postImgEntity = context.PostEntityImages["PostImg"];
}
else
{
throw new InvalidPluginExecutionException("Post Update image - PostImg does not exist!");
}
retrieveEntity = service.Retrieve(currentEntity.LogicalName, currentEntity.Id, new ColumnSet("ly_singletext2", "ly_singletext3"));
if (retrieveEntity.Contains("ly_singletext2"))
{
sb.Append("retrieveEntity contains attribute ly_singletext2 = ");
sb.Append(retrieveEntity.GetAttributeValue<string>("ly_singletext2"));
sb.Append("; ");
}
else
{
sb.Append("retrieveEntity does not contain attribute ly_singletext2");
sb.Append("; ");
}
if (currentEntity.Contains("ly_singletext2"))
{
sb.Append("currentEntity contains attribute ly_singletext2 = ");
sb.Append(currentEntity.GetAttributeValue<string>("ly_singletext2"));
sb.Append("; ");
}
else
{
sb.Append("currentEntity does not contain attribute ly_singletext2");
sb.Append("; ");
}
if (preImgEntity.Contains("ly_singletext2"))
{
sb.Append("preImgEntity contains attribute ly_singletext2 = ");
sb.Append(preImgEntity.GetAttributeValue<string>("ly_singletext2"));
sb.Append("; ");
}
else
{
sb.Append("preImgEntity does not contain attribute ly_singletext2");
sb.Append("; ");
}
if (postImgEntity.Contains("ly_singletext2"))
{
sb.Append("postImgEntity contains attribute ly_singletext2 = ");
sb.Append(postImgEntity.GetAttributeValue<string>("ly_singletext2"));
sb.Append("; ");
}
else
{
sb.Append("postImgEntity does not contain attribute ly_singletext2");
sb.Append("; ");
}
if (retrieveEntity.Contains("ly_singletext3"))
{
sb.Append("retrieveEntity contains attribute ly_singletext3 = ");
sb.Append(retrieveEntity.GetAttributeValue<string>("ly_singletext3"));
sb.Append("; ");
}
else
{
sb.Append("retrieveEntity does not contain attribute ly_singletext3");
sb.Append("; ");
}
if (currentEntity.Contains("ly_singletext3"))
{
sb.Append("currentEntity contains attribute ly_singletext3 = ");
sb.Append(currentEntity.GetAttributeValue<string>("ly_singletext3"));
sb.Append("; ");
}
else
{
sb.Append("currentEntity does not contain attribute ly_singletext3");
sb.Append("; ");
}
if (preImgEntity.Contains("ly_singletext3"))
{
sb.Append("preImgEntity contains attribute ly_singletext3 = ");
sb.Append(preImgEntity.GetAttributeValue<string>("ly_singletext3"));
sb.Append("; ");
}
else
{
sb.Append("preImgEntity does not contain attribute ly_singletext3");
sb.Append("; ");
}
if (postImgEntity.Contains("ly_singletext3"))
{
sb.Append("postImgEntity contains attribute ly_singletext3 = ");
sb.Append(postImgEntity.GetAttributeValue<string>("ly_singletext3"));
sb.Append("; ");
}
else
{
sb.Append("postImgEntity does not contain attribute ly_singletext3");
sb.Append("; ");
}
throw new InvalidPluginExecutionException(sb.ToString());
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in PostWorkOrderUpdate.", ex);
}
catch (Exception ex)
{
tracingService.Trace("PostWorkOrderUpdate unexpected exception: {0}", ex.Message);
throw;
}
}
}
}
}

插件注册信息如下:

我还为此Step注册了两个Image,一个为Pre阶段的Image,一个为Post阶段的Image,如下:

然后我进行了一系列测试:

如果两个字段都变化,获取到的消息如下:

retrieveEntity contains attribute ly_singletext2 = 5-New;
currentEntity contains attribute ly_singletext2 = 5-New;
preImgEntity contains attribute ly_singletext2 = 5;
postImgEntity contains attribute ly_singletext2 = 5-New;
retrieveEntity contains attribute ly_singletext3 = 总体满意度-New;
currentEntity contains attribute ly_singletext3 = 总体满意度-New;
preImgEntity contains attribute ly_singletext3 = 总体满意度;
postImgEntity contains attribute ly_singletext3 = 总体满意度-New;

如果只更改一个字段ly_singletext2的值,另外一个字段ly_singletext3不变,保存后获取的结果是:

retrieveEntity contains attribute ly_singletext2 = 5-New;
currentEntity contains attribute ly_singletext2 = 5-New;
preImgEntity contains attribute ly_singletext2 = 5;
postImgEntity contains attribute ly_singletext2 = 5-New;
retrieveEntity contains attribute ly_singletext3 = 总体满意度;
currentEntity does not contain attribute ly_singletext3;
preImgEntity contains attribute ly_singletext3 = 总体满意度;
postImgEntity contains attribute ly_singletext3 = 总体满意度;

如果只更改一个字段ly_singletext2的值,这里将其值清空,另外一个字段ly_singletext3不变,保存后获取的结果是:

retrieveEntity does not contain attribute ly_singletext2;
currentEntity contains attribute ly_singletext2 = ;
preImgEntity contains attribute ly_singletext2 = 5;
postImgEntity does not contain attribute ly_singletext2;
retrieveEntity contains attribute ly_singletext3 = 总体满意度;
currentEntity does not contain attribute ly_singletext3;
preImgEntity contains attribute ly_singletext3 = 总体满意度;
postImgEntity contains attribute ly_singletext3 = 总体满意度;

如果只更改一个字段ly_singletext2的值,本来该字段的值为空,这里设置其值为New,另外一个字段ly_singletext3的值一直为空保持不变,保存后获取的结果是:

retrieveEntity contains attribute ly_singletext2 = New;
currentEntity contains attribute ly_singletext2 = New;
preImgEntity does not contain attribute ly_singletext2;
postImgEntity contains attribute ly_singletext2 = New;
retrieveEntity does not contain attribute ly_singletext3;
currentEntity does not contain attribute ly_singletext3;
preImgEntity does not contain attribute ly_singletext3;
postImgEntity does not contain attribute ly_singletext3;

总结下:

  • Target参数转换后的实体会包括所有要设置新值的字段(属性),如果要设置的新值为空值,也会包括该字段(属性),只是该字段(属性)的值为空。
  • Target参数转换后的实体不会包括不更改的字段(属性)。
  • Pre Update Image包括了字段更新之前的值,如果之前该字段的值为空值,则Pre Update Image不会包括这个字段(属性);
  • Post Update Image包括了字段(属性)要更新的值,如果字段(属性)是更新为空值则Post Update Image不包括该字段,如果字段(属性)不更新的话,Post Update Image包括的就是这个字段(属性)的原值;
  • 在Post Update阶段查询当前实体的字段(属性)值,查询到的是要变更的新值,如果字段(属性)要要更新为空值,查询结果的字段(属性)集中不会包括该字段(属性),所以不用再查询,直接从Target参数转换后的实体的字段(属性)集合中获取。
  • 如果要看字段(属性)值在Update消息中是否变化的话,检查Target参数转换后的实体是否包括该字段(属性)即可,新值在Target参数转换后的实体的字段(属性)集合中,旧值可以用Pre Update Image来获取。
  • 获取Image之前先检查Image是否存在,分别用类似context.PreEntityImages.Contains("PreImg") 或者 context.PostEntityImages.Contains("PostImg")。
  • 获取字段(属性)值之前先检查字段(属性)的确存在,用类似 currentEntity.Contains("ly_singletext2") 的语法。

Dynamics 365 CE Update消息PostOperation阶段Image的尝试的更多相关文章

  1. Use SQL to Query Data from CDS and Dynamics 365 CE

    from : https://powerobjects.com/2020/05/20/use-sql-to-query-data-from-cds-and-dynamics-365-ce/ Have ...

  2. 使用Dynamics 365 CE Web API查询数据加点料及选项集字段常用查询

    微软动态CRM专家罗勇 ,回复336或者20190516可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me. 紧接上文:配置Postman通过OAuth 2 implicit ...

  3. Dynamics 365 CE中AsyncOperationBase表记录太多,影响系统性能怎么办?

    微软动态CRM专家罗勇 ,回复311或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文主要是根据微软官 ...

  4. Dynamics 365 CE将自定义工作流活动程序集注册到磁盘并引用其他类库

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  5. 安装完成Dynamics 365 CE后别忘了更改维护作业的运行时间

    摘要: 微软动态CRM专家罗勇 ,回复309或者20190308可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 安装完毕Dy ...

  6. Dynamics 365 CE中使用FetchXML进行聚合运算

    微软动态CRM专家罗勇 ,回复328或者20190429可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! Dynamics 365 Customer Engagement ...

  7. Dynamics 365 CE命令栏按钮点击后刷新表单页面方法

    微软动态CRM专家罗勇 ,回复326或者20190428可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! Dynamics 365 Customer Engagement ...

  8. 启用WCF压缩提升Dynamics 365 CE的网络性能

    摘要: 微软动态CRM专家罗勇 ,回复307或者20190308可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文系根据微 ...

  9. 做了面向互联网部署的Dynamics 365 CE更改AD FS的登录页面

    摘要: 微软动态CRM专家罗勇 ,回复306或者20190307可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 默认情况下A ...

随机推荐

  1. Blazor(WebAssembly) + .NETCore 实现斗地主

    之前群里大神发了一个 html5+ .NETCore的斗地主,刚好在看Blazor WebAssembly 就尝试重写试试. 还有就是有些标题党了,因为文章里几乎没有斗地主的相关实现:),这里主要介绍 ...

  2. JS---DOM---为元素解绑事件

    解绑事件 注意:用什么方式绑定事件, 就应该用对应的方式解绑事件 1.解绑事件 对象.on事件名字=事件处理函数--->绑定事件 对象.on事件名字=null;   //1 对象.on事件名字= ...

  3. 【AGC028D】Chord

    Problem Description 给定一个圆,圆上均等地放着 \(2n\) 个点,已有 \(k\) 对点之间连好了边,从中选择剩下 \(n-k\) 对点随意连边. 求所有连边方案中,联通块的个数 ...

  4. Angular常用VSCode插件

    1.Angular 8 Snippets(全家桶) 2.TSLint(ts代码规范.错误提示) 3.Material Icon Theme(文件图标) 4.One Dark Pro(主题) 5.Ang ...

  5. html5的 history模式和hash模式

    直观区别 hash 带一个# history 没有# 各自特点 hash: 仅 hash 符号之前的内容会被包含在请求中,**因此对于后端来说,即使没有做到对路由的全覆盖,也不会返回 404 错误.* ...

  6. Android 使用 aapt 命令查看 apk 包名

    一.aapt 是什么 aapt 即Android Asset Packaging Tool,在SDK的build-tools目录下.该工具可以查看,创建, 更新ZIP格式的文档附件(zip, jar, ...

  7. Quartznet速记

    参与 https://www.cnblogs.com/lzrabbit/archive/2012/04/14/2371420.html 1.XML配置 参考:https://www.cnblogs.c ...

  8. ActiveMQ下载与安装(消息中间件JMS)

    下载 官方网站下载:http://activemq.apache.org/ 1.3.2安装(Linux) (1)将apache-activemq-5.12.0-bin.tar.gz 上传至服务器 (2 ...

  9. ORA-16032和ORA-07286 LOG_ARCHIVE_DEST_1没生效

    主备切换在备库startup时出现归档路径没写到spfile里...注意:修改参数时最好带上scope=spfile或scope=both,以免重启出现异常.SQL> startup mount ...

  10. Oracle ASM无法识别扩展分区的磁盘设备

    在linux 环境下,我们一般通过udev或者asmlib来绑定磁盘分区作为ASM的候选存储单元.在使用udev的情况下,一般只要我们可以看到被绑定的磁盘的设备,并且这些设备的属主和权限没有问题,AS ...