1

誰かが私が間違っていることを教えてもらえますか?私は1週間以上試しています.

コードに従ってください。

プラグイン (実行) からの予期しない例外: Microsoft.Crm.Sdk.Samples.ProjectTotalAmount: System.Collections.Generic.KeyNotFoundException: 指定されたキーがディクショナリに存在しませんでした。

namespace Microsoft.Crm.Sdk.Samples
{
    public class ProjectTotalAmount : IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {

            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

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

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


                if (entity.LogicalName == "new_project")
                {


                    Guid projectGUID = ((EntityReference)entity["new_project"]).Id;
                    Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));

                    decimal totalAmount = 0;

                    try
                    {
                        //fetchxml to get the sum total of estimatedvalue
                        string new_amount_sum = string.Format(@" 
                        <fetch distinct='false' mapping='logical' aggregate='true'> 
                            <entity name='new_projectitem'> 
                                <attribute name='new_amount' alias='new_amount' aggregate='sum' /> 
                                <filter type='and'>
                                    <condition attribute='new_projectid' operator='eq' value='{0}' uiname='' uitype='' />
                                </filter>
                            </entity>
                        </fetch>", a.Id);

                        EntityCollection new_amount_sum_result = service.RetrieveMultiple(new FetchExpression(new_amount_sum));

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

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


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

        }
    }   
}

プラグインの設定:

検証後 同期実行モード サーバー展開

4

1 に答える 1

3

コードを確認する前に役立つヒントをいくつか...

  • このエラーは通常、コードが存在しない (または値を持たない) 属性を参照していることを意味します。
  • プラグインが登録されているメッセージについては言及していません。これは、実行時に使用可能なパラメーターに影響を与える可能性があります
  • 変数をコメントアウトしましたtracingServiceが、これは少なくともコードがどこまで進んだかを確認するのに役立ちます。元に戻し、このような行をいくつか追加して、障害が発生する前の進行状況を追跡します。この情報は、クライアント側の例外ダイアログで提供されるエラー ログに書き込まれます。

    tracingService.Trace("Project Id is {0}", projectGUID);` 
    

    tracingService.Trace("Number of returned records: {0}", new_amount_sum_result.Entities.Count);`
  • Id次の行は、属性fromのみを使用してaおり、これは既に次のように存在するため、完全に冗長に見えentity.Idます。

    Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));

于 2012-06-06T14:05:25.540 に答える