Dynamics 365 + Custom Workflow + Entity Reference 的 Id 和 Key 属性不能为空
Dynamics 365 + Custom Workflow + Entity Reference cannot have Id and Key Attributes empty
我有 2 个自定义工作流程。一个的输出成为另一个的输入。
在我调用第一个自定义工作流并在另一个步骤中使用它的输出后,出现错误:
'InValid Argument' error - Entity Reference cannot have Id and Key Attributes empty.
第一个工作流程的代码 -
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();
}
}
}
WorkOrderRecord 记录是一个 GUID。它不能为空。 if (WorkOrderRecord != null)
应该改为 if (WorkOrderRecord != Guid.Empty)
如果这被注册为在工作流中调用的自定义操作,则很可能您已根据需要指定了输出实体引用参数,但您的工作流返回 null。检查是否需要任何操作输出变量。如果是这样,将它们设置为可选,然后保存、发布、更新插件并尝试。这为我修复了错误。问题不在于代码,而在于期望返回某些内容的操作。
我有 2 个自定义工作流程。一个的输出成为另一个的输入。
在我调用第一个自定义工作流并在另一个步骤中使用它的输出后,出现错误:
'InValid Argument' error - Entity Reference cannot have Id and Key Attributes empty.
第一个工作流程的代码 -
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();
}
}
}
WorkOrderRecord 记录是一个 GUID。它不能为空。 if (WorkOrderRecord != null)
应该改为 if (WorkOrderRecord != Guid.Empty)
如果这被注册为在工作流中调用的自定义操作,则很可能您已根据需要指定了输出实体引用参数,但您的工作流返回 null。检查是否需要任何操作输出变量。如果是这样,将它们设置为可选,然后保存、发布、更新插件并尝试。这为我修复了错误。问题不在于代码,而在于期望返回某些内容的操作。