VS2010插件 - 错误1当前上下文中不存在名称“ExecutePostAccountUpdate”

时间:2014-02-10 15:03:32

标签: visual-studio-2010 plugins crm

我正在尝试创建一个插件来抓住机会的收入,并总计帐户实体的收入。我正在使用Visual Studio 2010 Professional并使用MS Dynamics CRM 2013 SDK。我一直在关注两个指南来帮助我实现目标,但它们适用于Dynamics CRM 2011.对于CRM 2013没有很多指南,因此我希望使用此CRM 2011指南来获取基本插件并进行修改满足我的需求。

1: http://mscrmshop.blogspot.com.au/2012/02/plugin-to-update-children-records-when.html

2: http://mscrmshop.blogspot.com/2012/04/how-to-update-parent-record-when-child.html

使用这些示例我在Visual Basic 2010上启动了插件,但是当我进行构建/部署时,会出现一些错误。错误1是我似乎无法过去的那个。

1 - 找不到类型或命名空间名称'FetchExpression'(您是否缺少using指令或程序集引用?)C:\ VS2010 \ AccountRevenue \ Plugins1 \ PostAccountUpdate.cs 114 95 Plugins1

2 - 找不到类型或命名空间名称'FetchExpression'(您是否缺少using指令或程序集引用?)C:\ VS2010 \ AccountRevenue \ Plugins1 \ PostAccountUpdate.cs 114 99 Plugins1

3 - 当前上下文中不存在名称“ExecutePostAccountUpdate”C:\ VS2010 \ AccountRevenue \ Plugins1 \ PostAccountUpdate.cs 51 154 Plugins1

    `// <copyright file="PostAccountUpdate.cs" company="">
// Copyright (c) 2014 All Rights Reserved
// </copyright>
// <author></author>
// <date>2/7/2014 2:53:23 PM</date>
// <summary>Implements the PostAccountUpdate Plugin.</summary>
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
// </auto-generated>
using Microsoft.Xrm.Sdk.Client; // to get the OrganizationContext
using System.Linq; // to use linq queries with OrganizationContext
namespace AccountRevenue.Plugins1
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;

    /// <summary>
    /// PostAccountUpdate Plugin.
    /// Fires when the following attributes are updated:
    /// All Attributes
    /// </summary>    
    public class PostAccountUpdate: Plugin
    {
        /// <summary>
        /// Alias of the image registered for the snapshot of the 
        /// primary entity's attributes before the core platform operation executes.
        /// The image contains the following attributes:
        /// rrm_accountrevenue
        /// </summary>
        private readonly string preImageAlias = "PreImage";

        /// <summary>
        /// Alias of the image registered for the snapshot of the 
        /// primary entity's attributes after the core platform operation executes.
        /// The image contains the following attributes:
        /// rrm_accountrevenue
        /// 
        /// Note: Only synchronous post-event and asynchronous registered plug-ins 
        /// have PostEntityImages populated.
        /// </summary>
        private readonly string postImageAlias = "PostImage";

        /// <summary>
        /// Initializes a new instance of the <see cref="PostAccountUpdate"/> class.
        /// </summary>
        public PostAccountUpdate()
            : base(typeof(PostAccountUpdate))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "account", new Action<LocalPluginContext>(ExecutePostAccountUpdate)));

            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
        }

        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostOpportunityCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }


            IPluginExecutionContext context = localContext.PluginExecutionContext;

            //Get a IOrganizationService
            IOrganizationService service = localContext.OrganizationService;

            //create a service context
            var ServiceContext = new OrganizationServiceContext(service);
            //ITracingService tracingService = localContext.TracingService;

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                //get the customerid
                EntityReference a = (EntityReference)entity.Attributes["customerid"];

                decimal totalAmount = 0;

                try
                {
                    //fetchxml to get the sum total of rrm_accountrevenue
                    string rrm_accountrevenue_sum = string.Format(@" 
            <fetch distinct='false' mapping='logical' aggregate='true'> 
                <entity name='opportunity'> 
                    <attribute name='rrm_accountrevenue' alias='rrm_accountrevenue_sum' aggregate='sum' /> 
                    <filter type='and'>
                        <condition attribute='statecode' operator='eq' value='Open' />
                            <condition attribute='customerid' operator='eq' value='{0}' uiname='' uitype='' />
                    </filter>
                </entity>
            </fetch>", a.Id);
                    EntityCollection rrm_accountrevenue_sum_result = service.RetrieveMultiple(new FetchExpression(rrm_accountrevenue_sum));

                    foreach (var c in rrm_accountrevenue_sum_result.Entities)
                    {
                        totalAmount = ((Money)((AliasedValue)c["rrm_accountrevenue_sum"]).Value).Value;
                    }

                    //updating the field on the account
                    Entity acc = new Entity("account");
                    acc.Id = a.Id;
                    acc.Attributes.Add("new_oppamount", new Money(totalAmount));
                    service.Update(acc);

                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
            }
        }
    }
}

    `

1 个答案:

答案 0 :(得分:0)

1,2 - 您需要添加对microsoft.xrm.sdk.dll和microsoft.crm.sdk.proxy.dll的引用。 How to add reference 你可以在SDK中找到dll。

3-你可以重载“执行”方法。

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
}

Good place to start write plugins