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

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

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

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

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

/// <summary>
/// 获取属性集合
/// </summary>
/// <param name="dataEntity">Entity</param>
/// <returns>返回属性集合</returns>
public AttributeCollection GetTriggerData(Entity dataEntity)
{
AttributeCollection parames = new AttributeCollection(); if (dataEntity != null)
{
//先将字段放在一个字符串数组中
string[] arrayStr = { "new_actiontype",
"new_po_status", "new_sort",
"new_detailitem","new_costfrom","new_bgcontrolperiod" };
//foreache 循环判断,把dataEntity的索引赋给parames中去。
foreach (var item in arrayStr)
{
if (dataEntity.Contains(item) && dataEntity[item] != null)
{
parames[item] = dataEntity[item];
}
}
}
return parames;
}
调用:AttributeCollection parmes = GetTriggerData(dataEntity);

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

/// <summary>
/// 根据所属预算品类、费用项目、费用归属和预算控制期间获取预算费用项实体
/// </summary>
/// <param name="service">crm组织服务</param>
/// <param name="parmes">属性集合</param>
/// <returns>返回Entity</returns>
public Entity GetBugetByParmes(IOrganizationService service, AttributeCollection parmes)
{
QueryByAttribute query = new QueryByAttribute("new_buget");查询的实体
query.ColumnSet = new ColumnSet("new_bugetid");//查询的列
query.AddAttributeValue("statecode", 0);//查询的条件
query.AddAttributeValue("new_sort", (parmes["new_sort"] as EntityReference).Id);
query.AddAttributeValue("new_expenseitem", (parmes["new_detailitem"] as EntityReference).Id);
query.AddAttributeValue("new_bugetunit", (parmes["new_costfrom"] as EntityReference).Id);
query.AddAttributeValue("new_bedgetsheet", (parmes["new_bgcontrolperiod"] as EntityReference).Id); EntityCollection acc_new_buget = service.RetrieveMultiple(query); Entity new_bugetEntity = null; if (acc_new_buget.Entities.Count > )
{
new_bugetEntity = acc_new_buget.Entities[] as Entity;
} return new_bugetEntity;
}

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

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

//查询
QueryByAttribute query = new QueryByAttribute("new_exp_undertaker");
query.ColumnSet = new ColumnSet("new_ratio_undertaker");
query.AddAttributeValue("new_pe_undertaker", new_promotion_peid); EntityCollection accEntityColls = service.RetrieveMultiple(query); if (accEntityColls.Entities.Count == ) return; foreach (Entity accEntity in accEntityColls.Entities)
{
Double? new_ratio_undertaker = accEntity.Contains("new_ratio_undertaker")
? accEntity["new_ratio_undertaker"] as Double? : new Double?();
//更新
UpdateNew_deficit(service, new_ratio_undertaker, new_writeoff, accEntity.Id);
}
取值:(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前期处理。这里暂不说明。

Entity targetEntity = context.InputParameters["Target"] as Entity;

//消息名称
string messageName = context.MessageName; decimal sumNew_budgetbalance = ; switch (messageName)
{
case "Create":
sumNew_budgetbalance = DoCreateSumNew_budgetbalance(targetEntity);
break;
case "Update":
sumNew_budgetbalance = DoUpdateSumNew_budgetbalance(targetEntity, preImageEntity);
break;
} targetEntity["new_budgetbalance"] = new Money(sumNew_budgetbalance); context.InputParameters["Target"] = targetEntity;//把提交的值放到Target中,当保存之后,就会把这些值保存到数据库中。 } .RegisterFile.crmregister: <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">
<Steps>
<clear />
<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">
<Images />
</Step>
<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">
<Images>
<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" />
</Images>
</Step>
</Steps>
</Plugin>

1

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

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

base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Create", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Update", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "Delete", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(, "SetStateDynamicEntity", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget))); protected void ExecuteNew_po_New_po_status_SumNew_tt_budget(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
} IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationServiceAll; Entity dataEntity = null;
switch (context.MessageName)
{
case "Create":
dataEntity = context.InputParameters["Target"] as Entity;
break;
case "Update":
case "SetStateDynamicEntity":
dataEntity = context.PostEntityImages[this.postImageAlias] as Entity;
break;
case "Delete":
dataEntity = context.PreEntityImages[this.preImageAlias] as Entity;
break;
} New_po_New_po_status_SumNew_tt_budgetAction(service, dataEntity); }

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

foreach (DataRow row in ds.Tables[].Rows)
{
if (!string.IsNullOrEmpty(row["new_sort"].ToString()))
{
new_sort = new Guid(row["new_sort"].ToString());
}
}

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

private readonly string preImageAlias = "image";
Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null; if(messageName =="Create")
{
if(preImageEntity != null)
{
if(preImageEntity.Contains("new_promotion_pe"))
{
new_promotion_peId = (preImageEntity["new_promotion_pe"] as EntityReference).Id;
}
}
}

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. redis 处理命令的过程

    redis版本:redis-3.2.9 在客户端输入 set name zhang,调试redis服务器,得到调用栈如下: 在dictReplace中加了断点,结果跳出来4个线程,redis还是单进程 ...

  2. idea Exception in thread "http-apr-8080-exec-2" java.lang.OutOfMemoryError: PermGen space

    idea Exception in thread "http-apr-8080-exec-2" java.lang.OutOfMemoryError: PermGen space ...

  3. UVALive 4764 dp

    DES: 这是一个新的游戏.给你一套牌.编号从1到100000.正常来说.你手中的牌和这次翻的牌是一样的,就会加一分.但是.如果是999的话.加三分.所以问你最大的分是多少. 貌似是简单的DP吧.(D ...

  4. ETL设计详解

    ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...

  5. vue项目启动

    这篇文章主要用于有源码vue项目安装: 1.安装node.js环境(npm包管理器)前面博客有写到如何安装: 2.vue-cli 脚手架构建工具前面博客有写到如何安装: 3.cnpm  npm的淘宝镜 ...

  6. 玩转X-CTR100 l STM32F4 l UCOS-III移植

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 概述 前后台系统 简单的小型系统设计一般是基于前后台 ...

  7. CentOS 6.5使用yum快速搭建LAMP环境

    由于这里采用yum方式安装,前提是我们必须配置好yum源.为了加快下载速度,建议使用网易的yum源. 这种方式对于初学者来说,非常方便,但是可定制性不强,而且软件版本较低.一般用于实验和学习环境. 1 ...

  8. js中的转换方法

    1.var colors =['red','blue','green']; console.log(colors.toString()); // red,blue,green 总结:toString( ...

  9. 算法训练 Multithreading

     算法训练 Multithreading   时间限制:1.0s   内存限制:256.0MB      问题描述 现有如下一个算法: repeat ni times yi := y y := yi+ ...

  10. 2.1 Linux中wait、system 分析

    wait与waitpid: 当子进程退出的时候,内核会向父进程发送SIGCHID信号,子进程的退出是一个异步事件(子进程可以在父进程运行的任何时刻终止). 子进程退出时,内核将子进程置为僵尸状态,这个 ...