用插件来实现实体的CRUD

通过插件来实现实体的CRUD。在日常的开发过程中,往往出现这样一类需求:在创建一条记录时判断是否符合某种条件。如果符合就通过程序对条记录进行增/删/改。实现该类需求的最好方式就是插件。

    涉及的实体

         客户         

         商机

需要实现的需求

       1.当创建一条客户记录时自动创建一条商机记录,并将该商机与该潜在客户关联起来

       2.当客户的名称更改时,自动更新与该客户关联的所有商机的名称

实现方案

1.在客户的create事件上创建一个插件,通过该插件创建商机记录
2.在客户的update事件上创建一个插件,通过该插件更新商机记录

技术分析

上面这些插件均需在如下条件下注册:

Event Pipleline Stage Of Execution:post-operation

Execution:synchronous


实现步骤

图1

图2

图3

图4


使用到的代码

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Text.RegularExpressions;  
  6.   
  7. using Microsoft.Xrm;  
  8. using Microsoft.Xrm.Sdk;  
  9. using Microsoft.Xrm.Sdk.Messages;  
  10. using Microsoft.Xrm.Sdk.Query;  
  11.   
  12. namespace Plugin17  
  13. {  
  14.     public class CRUD : IPlugin  
  15.     {  
  16.   
  17.         public void Execute(IServiceProvider serviceProvider)  
  18.         {  
  19.             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));  
  20.             IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  21.             IOrganizationService service = factory.CreateOrganizationService(null);  
  22.             Entity curEntity = (Entity)context.InputParameters["Target"];  
  23.   
  24.             string optMode = context.MessageName.ToLower();  
  25.   
  26.             if (optMode == "create")//创建事件  
  27.             {  
  28.                 Entity opportunity = new Entity();  
  29.                 opportunity.LogicalName = "opportunity";  
  30.                 opportunity.Attributes.Add("name", curEntity["name"].ToString() + "开启的商机" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));  
  31.                 opportunity.Attributes.Add("customerid"new EntityReference("account", curEntity.Id));  
  32.                 service.Create(opportunity);  
  33.   
  34.   
  35.             }  
  36.   
  37.             if (optMode == "update")//更新事件  
  38.             {  
  39.                 QueryExpression query = new QueryExpression();  
  40.                 query.EntityName = "opportunity";  
  41.                 query.ColumnSet = new ColumnSet(new string[] { "opportunityid""name" });  
  42.                 query.Criteria = new FilterExpression();  
  43.   
  44.                 ConditionExpression conditionExp = new ConditionExpression();  
  45.                 conditionExp.AttributeName = "customerid";  
  46.                 conditionExp.Operator = ConditionOperator.Equal;  
  47.                 conditionExp.Values.Add(curEntity.Id);  
  48.   
  49.                 EntityCollection qResult = service.RetrieveMultiple(query);  
  50.   
  51.   
  52.                 foreach (Entity tmp in qResult.Entities)  
  53.                 {  
  54.                     var tmpMatch = Regex.Match(tmp["name"].ToString(), ".+(开启的商机.+)");  
  55.                     if (tmpMatch.Success)  
  56.                     {  
  57.                         Entity uptEntity = new Entity();  
  58.                         uptEntity.LogicalName = "opportunity";  
  59.                         uptEntity.Attributes.Add("name", curEntity["name"].ToString() + tmpMatch.Groups[1].Value);  
  60.                         uptEntity.Id = tmp.Id;  
  61.                         service.Update(uptEntity);  
  62.                     }  
  63.   
  64.                 }  
  65.   
  66.   
  67.             }  
  68.         }  
  69.     }  
  70. }  

代码分析

    首先通过context.InputParameters["Target"]获取被操作的对象,然后判断引发该插件的操作类型(create或update)。这里我们使用了QueryExpression类来 对“商机”实体进行R操作,与上节用的FetchExpression不同。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值