1.??的使用,就是判断值是否为null,为null的话,给赋初值,否则就直接取值。

  1. decimal new_amount = 0;
  1. if (targetEntity.Contains("字段1"))
  2. {
  3. //?? 判断(targetEntity["字段1"] as Money为null的话,赋值为0
  4. new_amount = (targetEntity["字段1"] as Money ?? new Money(0M)).Value;
  5. }
  1. 注:字段1为货币(Money)类型。

2. 一般从Targt 中取值,必须先判断是否存在,然后在判断不为null.不然会报错,因为字段1有可能不在Target里面,意思

  1. targetEntity.Contains("字段1")为False,那么直接写targetEntity["字段1"] != null,就会报错。不信,大家可以试试。
  1. decimal new_amount = 0;
  1. if (targetEntity.Contains("字段1") && targetEntity["字段1"] != null)
  2. {
  3. new_amount = (targetEntity["字段1"] as Money).Value;
  4. }
  5. 而不能
  6. if (targetEntity["字段1"] != null && targetEntity.Contains("字段1"))
  7. {
  8. new_amount = (targetEntity["字段1"] as Money).Value;
  9. }
  1. 注:字段1为货币(Money)类型。

3.可以将Entity取出的字段放在AttributeCollection 属性集合中去,然后可以直接从这个属性集合中取值。

  1. /// <summary>
  2. /// 获取属性集合
  3. /// </summary>
  4. /// <param name="dataEntity">Entity</param>
  5. /// <returns>返回属性集合</returns>
  6. public AttributeCollection GetTriggerData(Entity dataEntity)
  7. {
  8. AttributeCollection parames = new AttributeCollection();
  9.  
  10. if (dataEntity != null)
  11. {
    //先将字段放在一个字符串数组中
  12. string[] arrayStr = { "new_actiontype",
  13. "new_po_status", "new_sort",
  14. "new_detailitem","new_costfrom","new_bgcontrolperiod" };
  15. //foreache 循环判断,把dataEntity的索引赋给parames中去。
  16. foreach (var item in arrayStr)
  17. {
  18. if (dataEntity.Contains(item) && dataEntity[item] != null)
  19. {
  20. parames[item] = dataEntity[item];
  21. }
  22. }
  23. }
  24. return parames;
  25. }
    调用:AttributeCollection parmes = GetTriggerData(dataEntity);

4.根据很多条件,查询一个实体的实体集合,除了fetchxml,也可以用QueryByAttribute。

  1. /// <summary>
  2. /// 根据所属预算品类、费用项目、费用归属和预算控制期间获取预算费用项实体
  3. /// </summary>
  4. /// <param name="service">crm组织服务</param>
  5. /// <param name="parmes">属性集合</param>
  6. /// <returns>返回Entity</returns>
  7. public Entity GetBugetByParmes(IOrganizationService service, AttributeCollection parmes)
  8. {
  9. QueryByAttribute query = new QueryByAttribute("new_buget");查询的实体
  10. query.ColumnSet = new ColumnSet("new_bugetid");//查询的列
  11. query.AddAttributeValue("statecode", 0);//查询的条件
  12. query.AddAttributeValue("new_sort", (parmes["new_sort"] as EntityReference).Id);
  13. query.AddAttributeValue("new_expenseitem", (parmes["new_detailitem"] as EntityReference).Id);
  14. query.AddAttributeValue("new_bugetunit", (parmes["new_costfrom"] as EntityReference).Id);
  15. query.AddAttributeValue("new_bedgetsheet", (parmes["new_bgcontrolperiod"] as EntityReference).Id);
  16.  
  17. EntityCollection acc_new_buget = service.RetrieveMultiple(query);
  18.  
  19. Entity new_bugetEntity = null;
  20.  
  21. if (acc_new_buget.Entities.Count > )
  22. {
  23. new_bugetEntity = acc_new_buget.Entities[] as Entity;
  24. }
  25.  
  26. return new_bugetEntity;
  27. }

5.Double? 类型+? ?是表示值类型可以为null,值类型的初始化不为null,比如int的初始值为0.

具体可以参考msdn:http://msdn.microsoft.com/zh-cn/library/1t3y8s4s(v=vs.90).aspx

  1. //查询
  2. QueryByAttribute query = new QueryByAttribute("new_exp_undertaker");
  3. query.ColumnSet = new ColumnSet("new_ratio_undertaker");
  4. query.AddAttributeValue("new_pe_undertaker", new_promotion_peid);
  5.  
  6. EntityCollection accEntityColls = service.RetrieveMultiple(query);
  7.  
  8. if (accEntityColls.Entities.Count == ) return;
  9.  
  10. foreach (Entity accEntity in accEntityColls.Entities)
  11. {
  12. Double? new_ratio_undertaker = accEntity.Contains("new_ratio_undertaker")
  13. ? accEntity["new_ratio_undertaker"] as Double? : new Double?();
  14. //更新
  15. UpdateNew_deficit(service, new_ratio_undertaker, new_writeoff, accEntity.Id);
  16. }
  17. 取值:(decimal)((new_ratio_undertaker.Value)

6. 如果一个实体上其他字段汇总到另外字段上,比如字段a,b,c,d 需要d = a+b*c,当进行这样子操作的时候,只有Create和Update,而且都为Pre_validation操作,能放到

Pre_validation(10)处理,尽量放到Pre_validation处理,因为这样子性能比较好,至于具体原因可以看SDK。

还有一般做check,或者导入数据根据一个lookup字段的带出值,赋给另外一个字段,也可以放在这里处理。CRM4的话是context.Stage == 10,Pre前期处理。这里暂不说明。

  1. Entity targetEntity = context.InputParameters["Target"] as Entity;
  2.  
  3. //消息名称
  4. string messageName = context.MessageName;
  5.  
  6. decimal sumNew_budgetbalance = ;
  7.  
  8. switch (messageName)
  9. {
  10. case "Create":
  11. sumNew_budgetbalance = DoCreateSumNew_budgetbalance(targetEntity);
  12. break;
  13. case "Update":
  14. sumNew_budgetbalance = DoUpdateSumNew_budgetbalance(targetEntity, preImageEntity);
  15. break;
  16. }
  17.  
  18. targetEntity["new_budgetbalance"] = new Money(sumNew_budgetbalance);
  19.  
  20. context.InputParameters["Target"] = targetEntity;//把提交的值放到Target中,当保存之后,就会把这些值保存到数据库中。
  21.  
  22. }
  23.  
  24. .RegisterFile.crmregister:
  25.  
  26. <Plugin Description="Plug-in to New_bugetUpdatenew_CalcuBudgetbalance" FriendlyName="New_bugetUpdatenew_CalcuBudgetbalance" Name="Frensworkz.LibyCrm.Plugins.New_bugetUpdatenew_CalcuBudgetbalance" Id="b5985a39-e284-e311-ad3b-00155d386b48" TypeName="Frensworkz.LibyCrm.Plugins.New_bugetUpdatenew_CalcuBudgetbalance">
  27. <Steps>
  28. <clear />
  29. <Step CustomConfiguration="" Name="New_bugetUpdatenew_CalcuBudgetbalance" Description="New_bugetUpdatenew_CalcuBudgetbalance Create" Id="b6985a39-e284-e311-ad3b-00155d386b48" MessageName="Create" Mode="Synchronous" PrimaryEntityName="new_buget" Rank="" SecureConfiguration="" Stage="PreOutsideTransaction" SupportedDeployment="ServerOnly">
  30. <Images />
  31. </Step>
  32. <Step CustomConfiguration="" Name="New_bugetUpdatenew_CalcuBudgetbalance" Description="New_bugetUpdatenew_CalcuBudgetbalance Update" Id="b9985a39-e284-e311-ad3b-00155d386b48" MessageName="Update" Mode="Synchronous" PrimaryEntityName="new_buget" Rank="" SecureConfiguration="" Stage="PreOutsideTransaction" SupportedDeployment="ServerOnly">
  33. <Images>
  34. <Image Attributes="new_quotabudget,new_ratio,new_saletarget,new_standardbuget,new_programholdbudget,new_noprogramhold,new_sales_app,new_pe_ch_money,new_po_ch_money,new_sales_release,new_adjust_fee,new_send_in,new_adjust_reduce" EntityAlias="PreImage" Id="bc985a39-e284-e311-ad3b-00155d386b48" MessagePropertyName="Target" ImageType="PreImage" />
  35. </Images>
  36. </Step>
  37. </Steps>
  38. </Plugin>

1

大家肯定会问,为什么会这样子写Plugin,这里暂不说明。

7.将明细中的金额汇总到主表上的一个字段时,很多人都没有考虑到SetStateDynamicEntity这个步骤。

  1. base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Create", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
  2. base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Update", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
  3. base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Delete", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
  4. base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "SetStateDynamicEntity", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
  5.  
  6. protected void ExecuteNew_po_New_po_status_SumNew_tt_budget(LocalPluginContext localContext)
  7. {
  8. if (localContext == null)
  9. {
  10. throw new ArgumentNullException("localContext");
  11. }
  12.  
  13. IPluginExecutionContext context = localContext.PluginExecutionContext;
  14.  
  15. IOrganizationService service = localContext.OrganizationServiceAll;
  16.  
  17. Entity dataEntity = null;
  18. switch (context.MessageName)
  19. {
  20. case "Create":
  21. dataEntity = context.InputParameters["Target"] as Entity;
  22. break;
  23. case "Update":
  24. case "SetStateDynamicEntity":
  25. dataEntity = context.PostEntityImages[this.postImageAlias] as Entity;
  26. break;
  27. case "Delete":
  28. dataEntity = context.PreEntityImages[this.preImageAlias] as Entity;
  29. break;
  30. }
  31.  
  32. New_po_New_po_status_SumNew_tt_budgetAction(service, dataEntity);
  33.  
  34. }

8.从DataRow中取值的时候,需要判断row["new_sort"].ToString()不能为空。

  1. foreach (DataRow row in ds.Tables[].Rows)
  2. {
  3. if (!string.IsNullOrEmpty(row["new_sort"].ToString()))
  4. {
  5. new_sort = new Guid(row["new_sort"].ToString());
  6. }
  7. }

9.新建(Create)的时候,需要判断preImageEntity != null。

  1. private readonly string preImageAlias = "image";
  2. Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
  3.  
  4. if(messageName =="Create")
  5. {
  6. if(preImageEntity != null)
  7. {
  8. if(preImageEntity.Contains("new_promotion_pe"))
  9. {
  10. new_promotion_peId = (preImageEntity["new_promotion_pe"] as EntityReference).Id;
  11. }
  12. }
  13. }

CRM 2011 Plugin 知识的总结加代码解释的更多相关文章

  1. crm 2011 plugin setparent setbusiness 用户更改经理 更改办事处

    背景: 在更改经理或者更改办事处时,使用plugin处理相应的团队. 问题:plugin写完,注册时发现使用update注册没有效果,然后bing得到,这里要使用setbusiness 和 setpa ...

  2. Microsoft Dynamics CRM 2011 Plugin中PluginExecutionContext.InputParameters["Target"]中的Target是从哪来的?

    图 1 如图1,CRM编程是一个请求响应模型,任何操作都是通过一个Request发起,一个Response返回结果,这个模型简单实用.所有请求类都是继承OrganizationRequest,所有响应 ...

  3. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(一)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/02/2336147.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  4. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(四)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/08/2343294.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  5. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(二)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2339490.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  6. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2340661.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  7. Dynamics 365 Customer Engagement导入解决方案时出错:Microsoft.Crm.CrmException: Plug-in assembly does not contain the required types or assembly content cannot be updated.

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

  8. Dynamics CRM 2011/2013 DeveloperToolkit的使用

    Dynamic CRM 2011的SDK中提供了一个叫DeveloperToolkit的工具,他的用途官方说明说的很明确,能方便开发者在VS中直接部署webresource.plugin.workfl ...

  9. Microsoft Dynamics CRM4.0 和 Microsoft Dynamics CRM 2011 JScript 方法对比

    CRM 2011 如果需要再IE里面调试,可以按F12在前面加上contentIFrame,比如 contentIFrame.document.getElementById("字段" ...

随机推荐

  1. Eclipse Indigo 3.7.0 安装GIT插件

    Eclipse上安装GIT插件EGit 首先打开Eclipse,然后点击Help>Install New Software>Add. Name:EGit Location: http:// ...

  2. AOJ1024 Cleaning Robot 2.0

    先说一说这个OJ:貌似是11区某大学ACM的OJ,叫AIZU ONLINE JUDGE,貌似还可以看到部分犇的代码...跪跪跪 然后知道这个OJ是某场比赛安利的= = 接下来将做法: 首先我们可以发现 ...

  3. BZOJ1461 字符串的匹配

    什么字符串...明明是两个数列... 分类上来讲,还是一道很好的noip题...(雾) 首先,kmp会不会?(答:会!) 其次,树状数组求顺序对会不会?(再答:会!) 讲完了!>.< 进入 ...

  4. o(1)取b > a,且b的二进制中1的个数等于a二进制中1的个数,且使b最小

    给你一个uint32 a,让你找到另一个uint32 b,使b > a,且b的二进制中1的个数等于a二进制中1的个数.且使b最小.(数据保证可出) 1 因为1的个数不变,所以必然大于n+lowb ...

  5. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  6. 玩转X-CTR100 | X-PrintfScope波形显示

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      X-CTR100控制器配套的X-Print ...

  7. ASP.NET下跨应用共享Session和使用Redis进行Session托管

    在之前的博客中,我说到了Session的共享问题,其中说到了Web Farm和Web Garden两种情况下Session的处理.在ASP.NET提供的Session处理方法中,有以下四种模式: 1. ...

  8. 使用 Koa + MongoDB + Redis 搭建论坛系统

    koa 相对于 express 的优势在于, 1.  使用了 yield generator 封装了co 框架, 使得异步处理, 能像同步那样书写 2.  使用了 中间件 ko-schema, 使得验 ...

  9. tp 邮件发送

    1.需要phpmail邮件发送包, 2.邮件发送函数function sendMail($to, $title, $content){ require_once('./PHPMailer_v5.1/c ...

  10. isKindOfClass in cocos2d-x

    在最新版2.*的cocos2d-x中isKindOfClass可以用如下代码代替. 未验证,不过看了引擎代码是这样写的   原代码 [s1 isKindOfClass:[DestHole class] ...