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" 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>>(40, "Create", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Delete", "new_po", new Action<LocalPluginContext>(ExecuteNew_po_New_po_status_SumNew_tt_budget)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "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[0].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;
   }
 }
}
时间: 2024-08-01 17:09:08

CRM 2011 Plugin 知识点小总结 plugin初学者必备知识的相关文章

Java集合的小抄 Java初学者必备

在尽可能短的篇幅里,将所有集合与并发集合的特征,实现方式,性能捋一遍.适合所有”精通Java”其实还不那么自信的人阅读. 不断更新中,请尽量访问博客原文. List ArrayList 以数组实现.节约空间,但数组有容量限制.超出限制时会增加50%容量,用System.arraycopy()复制到新的数组,因此最好能给出数组大小的预估值.默认第一次插入元素时创建大小为10的数组. 按数组下标访问元素–get(i)/set(i,e) 的性能很高,这是数组的基本优势. 直接在数组末尾加入元素–add

How to set up Dynamics CRM 2011 development environment

Recently I have been starting to learn Microsoft Dynamics CRM 2011 about implement plugin and workflow with SDK. The first thing I face is how to set up a development environment for Visual Studio. If you are using Visual Studio 2012 or lower version

Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynamics CRM 2011 2013 解决办法

Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynamics CRM 2011解决办法: John 25 Apr 2012 9:03 AM Yes thanks.  We were having this issue when importing a solution out of our development system that contain

开始使用CCA CRM 2011

你可能从微软的市场动态获知我们最近发布了最新版本的Microsoft Dynamics CRM 2011的客户关怀加速器(CCA R2).CCA在一个单一的用户界面提供呼叫中心功能相结合的,能够显示和操纵来自不同业务应用程序的数据.CCA提供了许多功能,包括: l 集成代理的桌面 l 脚本以消除重复的数据输入 l 计算机电话集成(CTI) l 代理活动报告 CCA的核心是一个允许开发人员构建自己的代理的桌面,并提供多会话管理等功能的框架.UI集成不同类型的应用程序(包括Web.Windows窗体

【转】This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in

原文网址:http://1982106a.blog.163.com/blog/static/8436495620149239361692/ 预览layout.xml文件时提示: This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in 导致无法正常预览布局文件: 问题根源:SDK版本过高,ADT版本低: 解决办法有好几种,如下:

CRM 2011: 报价(Quote Detail)到订单(Order Detail)的Mapping

CRM 2011: 报价(Quote Detail)到订单(Order Detail)的Mapping (MSCRM 2011 Mapping QuoteProduct to OrderProduct) 由于QuoteProduct 到 OrderProduct 的mapping是隐藏的,我们在solution管理里面是看不到它们之间的Mapping信息的,但是它们的关系是存在的. 我们怎么找到它们的关系呢? 在SQL里面查询如下语句: SELECT EntityMapId FROM Entit

Dynamices CRM JS 类库 神器 XrmServiceToolkit - A Microsoft Dynamics CRM 2011 &amp; CRM 2013 JavaScript Library

XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library http://xrmservicetoolkit.codeplex.com/documentation 特殊用法Create 和 Update { id: Id, logicalName: "new_entityname", type: "EntityReference" };       //Loo

Dynamics CRM 2011 权限管理(转)

http://www.cnblogs.com/LeoTang/p/3344265.html Dynamics CRM 2011 权限管理 CRM系统基于角色的权限主要通过部门.角色.用户.团队来 进行控制.每一个实体记录的所有者(Owner)必然是某一个用户或团队.一个用户或团队必然归属于一个且只归属于一个部门,但团队的成员即用户可以来自 不同的部门.用户和团队可以有多个不同的角色,并且用户或团队所被赋与的角色和它所在部门有关联,这种关联体现在对单个操作权限的五种层级选择,以商机的 读取为例:

Microsoft Dynamics CRM 2011 安装完全教程

环境介绍 WINDOWS 2008 R2 Datacenter Microsoft SQL SERVER 2008 R2 Microsoft Dynamics CRM 2011 准备工作 VM虚拟机中三台机器,三台机器最好是单独安装,如果是复制的会因为计算机系统的SID都是一样.所以想修改各个系统的SID号.以前WIN2003有修改SID的工具NEWSID,在WINDOWS2008系统集成了sysprep工具, 启动Windows2008进入系统后,打开“CMD窗 口”并进入到"C:\windo