fetchxml 汇总_CRM 2011 Plugin 知识点小总结 plugin初学者必备知识

本文总结了CRM 2011插件开发中关于FetchXML的使用,字段值判断以及Entity操作的一些关键知识点。包括使用??运算符初始化字段值,正确判断Target中字段是否存在并为非null,如何将Entity字段放入AttributeCollection,使用QueryByAttribute查询实体,以及处理Double?类型的字段。同时提到了在创建和更新操作中处理字段汇总的性能考虑。
摘要由CSDN通过智能技术生成

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

decimal new_amount = 0;

if (targetEntity.Contains("字段1"))

{

//?? 判断(targetEntity["字段1"] as Money为null的话,赋值为0new_amount = (targetEntity["字段1"] as Money ?? newMoney(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"] asMoney).Value;

}

而不能

if (targetEntity["字段1"] != null && targetEntity.Contains("字段1"))

{

new_amount = (targetEntity["字段1"] asMoney).Value;

}

注:字段1为货币(Money)类型。

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

//获取属性集合//Entity///返回属性集合publicAttributeCollection GetTriggerData(Entity dataEntity)

{

AttributeCollection parames = newAttributeCollection();

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];

}

}

}

returnparames;

}

调用:AttributeCollection parmes = GetTriggerData(dataEntity);

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

//根据所属预算品类、费用项目、费用归属和预算控制期间获取预算费用项实体//crm组织服务///属性集合///返回EntitypublicEntity 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"] asEntityReference).Id);

EntityCollection acc_new_buget =service.RetrieveMultiple(query);

Entity new_bugetEntity = null;

if (acc_new_buget.Entities.Count > 0)

{

new_bugetEntity = acc_new_buget.Entities[0] asEntity;

}

returnnew_bugetEntity;

}

5.Double? 类型+? 这个暂时不说明

//查询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 == 0) return;

foreach (Entity accEntity inaccEntityColls.Entities)

{

Double? new_ratio_undertaker = accEntity.Contains("new_ratio_undertaker")

? accEntity["new_ratio_undertaker"] as Double? : new Double?(0);

//更新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前期处理。这里暂不说明。

ba67af714a9364849b5de700800fd3cb.gif    

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

//消息名称string messageName =context.MessageName;

decimal sumNew_budgetbalance = 0;

switch(messageName)

{

case "Create":

sumNew_budgetbalance =DoCreateSumNew_budgetbalance(targetEntity);

break;

case "Update":

sumNew_budgetbalance =DoUpdateSumNew_budgetbalance(targetEntity, preImageEntity);

break;

}

targetEntity["new_budgetbalance"] = newMoney(sumNew_budgetbalance);

context.InputParameters["Target"] = targetEntity;//把提交的值放到Target中,当保存之后,就会把这些值保存到数据库中。}

2.RegisterFile.crmregister:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值