I have 2 custom workflows. The output of one becomes the input for the another.
After I call the first custom workflow and use the output of it in another step, it gives me error:
'InValid Argument' error - Entity Reference cannot have Id and Key Attributes empty.
Code for 1st workflow -
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;
}
}
Code for 2nd workflow -
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 record is a GUID. It can't be null. if (WorkOrderRecord != null)
should be changed to if (WorkOrderRecord != Guid.Empty)