none
CRM4.0插件 RRS feed

  • 问题

  • 给为前辈好,

          我在虚拟机上做了一个插件如下:

    /********************************************************
     代码功能说明:此plugin ,用于一个子实体的一个值自动更新一个父实体的汇总值,"Creat""Delete""Udpate"子实体时,就自动更新父实体的汇总值。
     注册说明:注册在子实体的"Creat""Delete""Udpate",其中“事件管道”为“Post Stage”。"Delete""Udpate" message需要注册“register new image"。
     */

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using CrmSdkQuery = Microsoft.Crm.Sdk.Query;

    // Enable easy access to our CrmService
    using MyCrmService = ProfessionalServicesPlugins.CrmSdk;

    namespace ProfessionalServicesPlugins
    {
        public class CreateWorkItemPlugin : IPlugin
           
        {
            public void Execute(IPluginExecutionContext context)
            {
                #region IPlugin 成员

                DynamicEntity preEntity=null;
                DynamicEntity Entity = null;
                MyCrmService.CrmService crmService = GetCrmService();

     

                if (context.MessageName == "Create")
                {
                    if (context.InputParameters.Properties.Contains("Target") &&
                            context.InputParameters.Properties["Target"] is DynamicEntity)
                    {
                        preEntity = (DynamicEntity)context.InputParameters.Properties["Target"];
                    }

                    if (context.PrimaryEntityName == MyCrmService.EntityName.new_workitem.ToString())
                    {
                        // Hours for the work Item
                        CrmNumber workItemHours = preEntity.Properties["new_budgetedhours"] as CrmNumber;

                        MyCrmService.new_project project = new MyCrmService.new_project();
                        MyCrmService.ColumnSet cols = new MyCrmService.ColumnSet();
                        cols.Attributes = new string[] { "new_projectid", "new_totalhoursspent" };

                        Guid projectid = ((Lookup)preEntity.Properties["new_projectid"]).Value;

                        project = crmService.Retrieve(MyCrmService.EntityName.new_project.ToString(),
                                                    projectid,
                                                    cols) as MyCrmService.new_project;

                        // Update the Project Total Hours
                        if (project.new_totalhoursspent == null)
                        {
                            project.new_totalhoursspent = new MyCrmService.CrmNumber();
                            project.new_totalhoursspent.Value = 0;
                        }

                        int totalHours = project.new_totalhoursspent.Value + workItemHours.Value;

                        project.new_totalhoursspent.Value = totalHours;

                        crmService.Update(project);


                    }
                }


                    if (context.MessageName == "Delete")
                    {
                        //取实体快照的属性,"PreImageEntity"为实体快照的名称,
                        //是在plugins中注册时“register new image"中的“别名”,用于储存执行操作前实体的属性。

                        preEntity = (DynamicEntity)context.PreEntityImages["PreImageEntity"];

                      
                       MyCrmService.new_project project = new MyCrmService.new_project();
                       MyCrmService.ColumnSet cols = new MyCrmService.ColumnSet();
                       cols.Attributes = new string[] { "new_projectid", "new_totalhoursspent" };

                       Guid projectid = ((Lookup)preEntity.Properties["new_projectid"]).Value;

                       project = crmService.Retrieve(MyCrmService.EntityName.new_project.ToString(),
                                                  projectid,
                                                 cols) as MyCrmService.new_project;

                                          

                       int preCharge;
                       preCharge = ((CrmNumber)preEntity.Properties["new_budgetedhours"]).Value;

                       int totalHours = project.new_totalhoursspent.Value - preCharge;
                    
                       project.new_totalhoursspent.Value = totalHours;

                      

                        crmService.Update(project);
                   
                   }

                    if (context.MessageName == "Update")
                    {
                        preEntity = (DynamicEntity)context.PreEntityImages["PreImageEntity"];

                        Entity = (DynamicEntity)context.InputParameters.Properties["Target"];

                        CrmNumber workItemHours = Entity.Properties["new_budgetedhours"] as CrmNumber;

                        MyCrmService.new_project project = new MyCrmService.new_project();
                        MyCrmService.ColumnSet cols = new MyCrmService.ColumnSet();
                        cols.Attributes = new string[] { "new_projectid", "new_totalhoursspent" };

                        Guid projectid = ((Lookup)preEntity.Properties["new_projectid"]).Value;

                        project = crmService.Retrieve(MyCrmService.EntityName.new_project.ToString(),
                                                   projectid,
                                                  cols) as MyCrmService.new_project;

                        int preCharge;
                        preCharge = ((CrmNumber)preEntity.Properties["new_budgetedhours"]).Value;

                        int totalHours = project.new_totalhoursspent.Value - preCharge + workItemHours.Value;

                        project.new_totalhoursspent.Value = totalHours;

     

                        crmService.Update(project);
                    }


            }         
           
             

                  private MyCrmService.CrmService GetCrmService()
            {
                // Setup the Authentication Token
                MyCrmService.CrmAuthenticationToken token = new MyCrmService.CrmAuthenticationToken();
                token.OrganizationName = "crm";

                MyCrmService.CrmService crmService = new MyCrmService.CrmService();
                crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                crmService.CrmAuthenticationTokenValue = token;

                return crmService;
            }
           }
         }
      

            #endregion

    当update“new_budgetedhours”字段时,一切正常,但修改这个表单实体中除这个字段之外的信息时,就会报错“给定关键字不在字典中”我不知道如何修改。请教一下给为前辈。谢谢。

     

     

     

     

     

     

     

     

     

    2011年7月20日 7:14

全部回复

  • Entity = (DynamicEntity)context.InputParameters.Properties["Target"]; CrmNumber workItemHours = Entity.Properties["new_budgetedhours"] as CrmNumber; 在update操作中,如果没有对new_budgetedhours进行更新时,在Entity中是获取不到这个字段的,在Entity中只保存的是当前记录的一些关键字段信息(记录的主键等)和在界面上进行修改过的字段信息。 你没有对new_budgetedhours更新,则在Entity中获取不到的。这里需要去做判断确保在Entity中包含了new_budgetedhours字段,再去做后续的逻辑计算。
    2011年7月21日 2:38
  • 嗯,谢谢您。
    2011年7月21日 3:26