2013-09-25 85 views
1

我正在构建自定义潜在客户资格认证的工作流程。 我在CRM中构建了一个工作流程,并且正在调用通过引导参考作为输入参数的自定义工作流活动,并且我期望创建的帐户的引用作为返回参数。CRM 2011工作流程中的DynamicActivity验证错误

的代码是:

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> 

为什么发生这种情况的任何想法?这似乎很普通的代码我

+0

你没有分配任何东西给出参数。下面一行在你的代码中被注释掉了:'accountRef.Set(executionContext,new EntityReference(“account”,accountId));' – Scorpion

+0

我评论过它,因为我认为这是一个问题。我删除了评论斜线,错误仍然存​​在。我已经更新了问题中的代码。 – MaPi

+0

您是否尝试过调试代码活动以检查其下降的位置? – Scorpion

回答

3

在参数定义你有InOutArgument装饰只有输入属性

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

更改类型InArgument

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

将解决这个问题。