Dynamics 365 + 自定义工作流程 + 实体引用的 Id 和 Key 属性不能为空

问题描述 投票:0回答:3

我有 2 个自定义工作流程。一个的输出成为另一个的输入。

在我调用第一个自定义工作流程并在另一个步骤中使用它的输出后,它给了我错误:

“无效参数”错误 - 实体引用的 Id 和键属性不能为空。

第一个工作流程的代码 -

public class RetrieveCaseForUnit : WorkFlowActivityBase
{
    #region Input Parameters
    [Input("Unit")]
    [ReferenceTarget(msdyn_customerasset.EntityLogicalName)]
    public InArgument<EntityReference> Unit { get; set; }
    #endregion

    #region Output Parameters
    [Output("Case")]
    [ReferenceTarget(Incident.EntityLogicalName)]
    public OutArgument<EntityReference> Case{ get; set; }
    #endregion


    public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
    {
        try
        {

            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext is null");
            }

            crmWorkflowContext.Trace("Getting Unit Input");
            EntityReference unitRef = Unit.Get<EntityReference>(executionContext);
            if (unitRef == null)
            {
                crmWorkflowContext.Trace("Error Message : Unit value not provided");
                throw new ArgumentNullException("Unit value not provided");
            }

            EntityReference caseRef = GetCase(crmWorkflowContext, unitRef);

            if (caseRef != null)
            {
                Case.Set(executionContext, caseRef);
            }
            else
            {
                Case.Set(executionContext, null);
            }
        }
        catch (Exception ex)
        {
            throw new InvalidWorkflowException();
        }
    }

    private static EntityReference GetCase(LocalWorkflowContext crmWorkflowContext, EntityReference unitRef)
    {
        EntityReference caseRef = null;

        CrmServiceContext serviceContext = new CrmServiceContext(crmWorkflowContext.OrganizationService);
        var caseRecord = (from currentcase in serviceContext.IncidentSet
                          where currentcase.gsscore_Unitid.Id == unitRef.Id 
                              &&  currentcase.gsscore_CaseTypeId.Id == new Guid("3A152B94-D2DF-E711-A94A-000D3A30DB97")
                          orderby currentcase.CreatedOn descending
                          select currentcase).FirstOrDefault();

        crmWorkflowContext.Trace("Case Record" + caseRecord);

        if (caseRecord != null)
            caseRef = caseRecord.ToEntityReference();

        return caseRef;
    }
}

第二个工作流程的代码 -

public class NMSGetWorkOrderForCase : WorkFlowActivityBase
{
    #region Input Parameters
    [Input(gsscore_nmsmessage.Fields.gsscore_CaseId)]
    [ReferenceTarget(Incident.EntityLogicalName)]
    public InArgument<EntityReference> Case { get; set; }
    #endregion

    #region Output Parameters
    [Output(gsscore_nmsmessage.Fields.WorkOrder)]
    [ReferenceTarget(msdyn_workorder.EntityLogicalName)]
    public OutArgument<EntityReference> NMSWorkOrder { get; set; }
    #endregion

    public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
    {
        if (crmWorkflowContext == null)
        {
            throw new ArgumentNullException("crmWorkflowContext is Null");
        }


        if (Case.Get(executionContext) == null)
        {
            crmWorkflowContext.UserId = crmWorkflowContext.WorkflowExecutionContext.UserId;
            throw new InvalidWorkflowException();
        }

        crmWorkflowContext.Trace("Start");
        Guid caseId = Case.Get<EntityReference>(executionContext).Id;
        try
        {
            CrmServiceContext serviceContext = new CrmServiceContext(crmWorkflowContext.OrganizationService);
            var WorkOrderRecord = (from currentworkoder in serviceContext.msdyn_workorderSet
                                   where currentworkoder.msdyn_ServiceRequest.Id == caseId
                                   orderby currentworkoder.CreatedOn descending
                                   select currentworkoder.Id
                                      ).ToList().FirstOrDefault();

           if (WorkOrderRecord != null)
            {
                EntityReference workorderRef = new EntityReference(msdyn_workorder.EntityLogicalName, WorkOrderRecord);
                NMSWorkOrder.Set(executionContext, workorderRef);
            }
        }
        catch (Exception ex)
        {
            crmWorkflowContext.TracingService.Trace("Case record does not exist." + crmWorkflowContext.WorkflowExecutionContext.MessageName + ex.Message);
            if (crmWorkflowContext.ErrorCode == null)
            {
                crmWorkflowContext.ErrorCode = ((int)WorkflowActivityErrorCode.NMSGetWorkOrderForCaseError).ToString();
            }
            crmWorkflowContext.UserId = crmWorkflowContext.WorkflowExecutionContext.UserId;
            throw new InvalidWorkflowException();
        }

    }
}
dynamics-crm dynamics-crm-online dynamics-crm-365
3个回答
1
投票

如果这注册为工作流中调用的自定义操作,则您可能已根据需要指定了输出实体引用参数,但您的工作流返回 null。检查是否需要任何操作输出变量。如果是这样,请将它们设置为可选,保存,发布,更新插件,然后尝试。这为我解决了错误。问题不在于代码,而在于期望返回某些内容的操作。


0
投票

WorkOrderRecord 记录是一个 GUID。它不能为空。

if (WorkOrderRecord != null)
应更改为
if (WorkOrderRecord != Guid.Empty)


0
投票

如果在退出

EntityReference
方法之前未使用 null 值初始化
Execute
的输出参数,可能会出现具有相同错误消息的非常相似的情况:

[Output("Found salesorder")]
[ReferenceTarget("salesorder")]
public OutArgument<EntityReference> Salesorder { get; set; }

...

Salesorder.Set(executionContext, null); /* you need this initialization */

if (isSalesOrderFound)
{
  Salesorder.Set(executionContext, so);
}

希望这对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.