Dynamics 365 CE Update消息PostOperation阶段Image的尝试
我是微软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的尝试的更多相关文章
- 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 ...
- 使用Dynamics 365 CE Web API查询数据加点料及选项集字段常用查询
微软动态CRM专家罗勇 ,回复336或者20190516可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me. 紧接上文:配置Postman通过OAuth 2 implicit ...
- Dynamics 365 CE中AsyncOperationBase表记录太多,影响系统性能怎么办?
微软动态CRM专家罗勇 ,回复311或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文主要是根据微软官 ...
- Dynamics 365 CE将自定义工作流活动程序集注册到磁盘并引用其他类库
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 安装完成Dynamics 365 CE后别忘了更改维护作业的运行时间
摘要: 微软动态CRM专家罗勇 ,回复309或者20190308可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 安装完毕Dy ...
- Dynamics 365 CE中使用FetchXML进行聚合运算
微软动态CRM专家罗勇 ,回复328或者20190429可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! Dynamics 365 Customer Engagement ...
- Dynamics 365 CE命令栏按钮点击后刷新表单页面方法
微软动态CRM专家罗勇 ,回复326或者20190428可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! Dynamics 365 Customer Engagement ...
- 启用WCF压缩提升Dynamics 365 CE的网络性能
摘要: 微软动态CRM专家罗勇 ,回复307或者20190308可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文系根据微 ...
- 做了面向互联网部署的Dynamics 365 CE更改AD FS的登录页面
摘要: 微软动态CRM专家罗勇 ,回复306或者20190307可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 默认情况下A ...
随机推荐
- webpack 配置babel-loader babel7
babel 7版本配置 在webpack中 默认只能处理部分 ES6的新语法,一些更高级的ES6或ES7的语法,webpack是处理不了的这个时候就需要借助第三方的loader 来帮助webpack ...
- webpack4 配置
package.json 开发环境/生产环境 webpack.config.js
- XNginx升级记录
之前的博文提到过,XNginx - nginx 集群可视化管理工具, 开发完成后一直稳定运行,直到前面因为一个站点的proxy站点配置问题,导致需要修改nginx 配置文件模板,因此借此机会对系统做了 ...
- 精通awk系列(4):awk用法入门
回到: Linux系列文章 Shell系列文章 Awk系列文章 awk用法入门 awk 'awk_program' a.txt awk示例: # 输出a.txt中的每一行 awk '{print $0 ...
- Mvc导入
[HttpPost] public void Import() { //获取文件 HttpPostedFileBase fileBase = Request.Files["file" ...
- form表单input回车提交问题
问题:文本框输入完成后点击回车页面刷新问题出在form上,当表单中只有一个文本框的时候获取焦点并点击回车之后会提交表单内容,就会发生刷新事件. 解决方法: 1.增加一个隐藏的输入框 <input ...
- PHP $_SERVER超全局变量详解
参考资料:https://www.php.net/manual/zh/reserved.variables.server.php $_SERVER 是一个包含了诸如头信息(header).路径(pat ...
- 属性文件——Java&Spring
属性文件 什么是属性文件 ? 定义:一个扩展名为properties文件,属性文件都是以key-value(键值对)来保存文件的内容,如:log4j.properties,db.properties等 ...
- [MySQL] 使用force index强制使用索引
1.在测试一个按照时间的范围查询时,尽管增加了索引,发现使用不到索引,可以使用这个来强制使用索引 测试过程为,创建下面的表,以及创建了联合索引 create table delay_delete_us ...
- 如何通过QT designer设置不让窗口最大化
最近使用QT写一个小窗口的程序,窗口通过QT designer制作之后,运行时可以最大化操作,且最大化之后界面上控件也不会随窗口变化而变化,但由于人都比较懒,直接在QT designer设置窗口属性时 ...