2013-09-25 42 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; } 

將解決這個問題。