1

私はカスタム リードのクオリフィケーションのワークフローを構築していました。CRM でワークフローを構築し、リード参照を入力パラメーターとして渡してカスタム ワークフロー アクティビティを呼び出しています。作成されたアカウントの参照を戻りパラメーターとして期待しています。

コードは次のとおりです。

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using System.Activities;
using Microsoft.Crm.Sdk.Messages;

namespace Quicksale
{
  /// <summary>
  /// Provide the functionality to qualify a lead
  /// </summary>
  public class QualifyLead : CodeActivity
  {
    #region Parameters
    [Input("EntityReference leadIn")]
    [ReferenceTarget("lead")]
    public InOutArgument<EntityReference> LeadReference { get; set; }


    [Output("newAccount")]
    [ReferenceTarget("account")]
    [RequiredArgument]
    public OutArgument<EntityReference> accountRef { get; set; }

    #endregion Parameters

    public Guid existingAccount(Guid leadId, IOrganizationService service)
    {

        QueryExpression accQuery = new QueryExpression("account");
        ColumnSet accColumnSet = new ColumnSet("accountid");

        accQuery.ColumnSet = accColumnSet;
        accQuery.Criteria = new FilterExpression();
        accQuery.Criteria.FilterOperator = LogicalOperator.Or;
        accQuery.Criteria.AddCondition("originatingleadid", ConditionOperator.Equal, leadId);
        accQuery.Criteria.AddCondition("custom_editableoriginatinglead", 
                                         ConditionOperator.Equal, leadId);

        EntityCollection retrieved = service.RetrieveMultiple(accQuery);
        Guid retrievedAccount = Guid.Empty;
        //  Iterate through returned collection.

        foreach (var c in retrieved.Entities)
        {
            retrievedAccount = new Guid(c.Attributes["accountid"].ToString());
            return retrievedAccount;
        }
        return Guid.Empty;
    }

    public Guid existingContact(Guid leadId, IOrganizationService service)
    {

        QueryExpression contQuery = new QueryExpression("contact");
        ColumnSet contColumnSet = new ColumnSet("contactid");

        contQuery.ColumnSet = contColumnSet;
        contQuery.Criteria = new FilterExpression();
        contQuery.Criteria.FilterOperator = LogicalOperator.Or;
        contQuery.Criteria.AddCondition("originatingleadid", 
                                         ConditionOperator.Equal, leadId);
        contQuery.Criteria.AddCondition("custom_editableoriginatinglead",
                                         ConditionOperator.Equal, leadId);
        EntityCollection retrieved = service.RetrieveMultiple(contQuery);
        Guid retrievedContactId = Guid.Empty;
        //  Iterate through returned collection.

        foreach (var c in retrieved.Entities)
        {
            retrievedContactId = new Guid(c.Attributes["contactid"].ToString());
            return retrievedContactId;
        }
        return Guid.Empty;
    }

    /// <summary>
    /// Overridden Execute() function to provide functionality to the workflow.
    /// </summary>
    /// <param name="executionContext">Execution context of the Workflow</param>
    /// <returns></returns>
    protected override void Execute(CodeActivityContext executionContext)
    {
      #region context
      //Create the context
      IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
      IOrganizationServiceFactory serviceFactory = 
                      executionContext.GetExtension<IOrganizationServiceFactory>();
      IOrganizationService service = 
                      serviceFactory.CreateOrganizationService(context.UserId);
      ITracingService tracingService = executionContext.GetExtension<ITracingService>();
      #endregion context
      tracingService.Trace("Retrieving lead...");
      Entity leadEntity = Helper.ActualEntity(LeadReference.Get(executionContext), service);
      tracingService.Trace("Lead retrieved.");


      tracingService.Trace("Qualifying lead...");
      QualifyLeadRequest req = new QualifyLeadRequest();
      Guid leadId = new Guid(leadEntity.Attributes["leadid"].ToString());
      req.LeadId = new EntityReference("lead", leadId);
      //Set the lead to qualified status
      req.Status = new OptionSetValue(3);
      //choose what records to create from the lead
      tracingService.Trace("Checking if lead has already been qualified as an account...");
      Guid accountId = existingAccount(leadId, service);
      tracingService.Trace("If lead has already been qualified as an account checked.");
      req.CreateAccount = accountId == Guid.Empty;
      tracingService.Trace("Checking if lead has already been qualified as a contact...");
      req.CreateContact = existingContact(leadId,service)==Guid.Empty;
        tracingService.Trace("If lead has already been qualified as a contact checked.");
      req.CreateOpportunity = false;

      QualifyLeadResponse resp = (QualifyLeadResponse)service.Execute(req);

      tracingService.Trace("Lead qualified.");
      tracingService.Trace("Retrieving account id...");
      if (accountId == Guid.Empty) {
          accountId = existingAccount(leadId, service);
      }
      tracingService.Trace("Account id retrieved.");
      tracingService.Trace("Setting output account reference...");
      accountRef.Set(executionContext, new EntityReference("account", accountId));
      tracingService.Trace("Output account reference set.");
    }
  }
}

ワークフローを実行すると、次のエラーが発生します。

Unhandled Exception:  System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, 
Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]:
    The following errors were encountered while processing the workflow tree:&#13;&#10;&#39;
    DynamicActivity&#39;: The private implementation of activity &#39;1: 
    DynamicActivity&#39; has the following validation error:   Compiler error(s) encountered processing expression 
    &quot;DirectCast(CustomActivityStep13_1_converted, Microsoft.Xrm.Sdk.EntityReference)&quot;.Invalid L-value expression.:Reference expressions cannot end with Conversion. 
   The provided expression&#39;s type must exactly match the type T of VisualBasicReference&lt;T&gt;
    or LambdaReference&lt;T&gt;.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220970</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>The following errors were encountered while processing the workflow tree:&amp;#13;&amp;#10;&amp;#39;DynamicActivity&amp;#39;: The private implementation of activity &amp;#39;1: DynamicActivity&amp;#39; has the following validation error:   Compiler error(s) encountered processing expression &amp;quot;DirectCast(CustomActivityStep13_1_converted, Microsoft.Xrm.Sdk.EntityReference)&amp;quot;.Invalid L-value expression.:Reference expressions cannot end with Conversion. The provided expression&amp;#39;s type must exactly match the type T of VisualBasicReference&amp;lt;T&amp;gt; or LambdaReference&amp;lt;T&amp;gt;.</Message>
  <Timestamp>2013-09-25T08:40:17.2376169Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>Unhandled Exception: System.Activities.InvalidWorkflowException: The following errors were encountered while processing the workflow tree:
'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "DirectCast(CustomActivityStep13_1_converted, Microsoft.Xrm.Sdk.EntityReference)".Invalid L-value expression.:Reference expressions cannot end with Conversion. The provided expression's type must exactly match the type T of VisualBasicReference&lt;T&gt; or LambdaReference&lt;T&gt;.
   at System.Activities.Hosting.WorkflowInstance.ValidateWorkflow(WorkflowInstanceExtensionManager extensionManager)
   at System.Activities.WorkflowApplication.EnsureInitialized()
   at System.Activities.WorkflowApplication.Enqueue(InstanceOperation operation, Boolean push)
   at System.Activities.WorkflowApplication.WaitForTurn(InstanceOperation operation, TimeSpan timeout)
   at System.Activities.WorkflowApplication.InternalRun(TimeSpan timeout, Boolean isUserRun)
   at Microsoft.Crm.Workflow.SynchronousRuntime.ActivityHost.StartWorkflowExecution(Activity workflow, ICommonWorkflowContext context)
   at Microsoft.Crm.Workflow.SynchronousRuntime.ActivityHost.StartWorkflow(ICommonWorkflowContext context)
</Message>
    <Timestamp>2013-09-25T08:40:17.2376169Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>

なぜこれが起こっているのかについてのアイデアはありますか? それは私にはかなり普通のコードのようです

4

1 に答える 1

4

引数の定義では、入力属性のみで装飾されたInOutArgumentがあります

[Input("EntityReference leadIn")]
[ReferenceTarget("lead")]
public InOutArgument<EntityReference> LeadReference { get; set; }

タイプをInArgumentに変更する

public InArgument<EntityReference> LeadReference { get; set; }

この問題を修正します。

于 2013-09-27T18:51:53.907 に答える