CRM 2011 Plugin 知识点小总结 plugin初学者必备知识
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 > 0)
{
new_bugetEntity = acc_new_buget.Entities[0] as Entity;
}
return new_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 in accEntityColls.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前期处理。这里暂不说明。
Entity targetEntity = context.InputParameters["Target"] as Entity;
//消息名称
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"] = new Money(sumNew_budgetbalance);
context.InputParameters["Target"] = targetEntity;//把提交的值放到Target中,当保存之后,就会把这些值保存到数据库中。
}
2.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="1" 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="1