CRM 2011工作流中的DynamicActivity验证错误

时间:2013-09-25 08:46:25

标签: dynamics-crm-2011

我正在构建自定义潜在客户资格赛的工作流程。 我在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>

关于为什么会发生这种情况的任何想法?对我来说这似乎是非常规则的代码

1 个答案:

答案 0 :(得分:4)

在参数定义中,您只能使用输入属性

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

将类型更改为 InArgument

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

将解决此问题。