web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics 365 + Custom Workflow + Entity Reference cannot have Id and Key Attributes empty.

(0) ShareShare
ReportReport
Posted on by 95

Hi,

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 '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 GetWorkOrderForCase : 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();
}

}
}

Any help would be appreciated.

Thanks in Advance

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    imayur Profile Picture
    630 on at

    Hi,

    In your first custom workflow you are querying on incidents which will return Entity type, but you need to return EntityReference, so cast it to EntityReference(not sure about  ToEntityReference())

    You should try like this

    caseRef =  new EntityReference("incident",caseRecord.Id);

    and set caseRef to output parameter.

    And you need to check Outputs of first workflow in step before calling second workflow

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi crm_fan,

    the error has been highlighted

    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){
    ...
    }

    You are executing the following instructions:

    • Query work order list and get workorder ID or default value
    • Default value for Guid is Guid.Empty, not "null"
    • So WorkOrderRecord != null is always right and you get an error when you set an empty guid.

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi crm_fan,

    if you solved the problem thanks to an answer, please mark it as verified to encourage the community to provide more and more a better support. Thank you. Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi crm_fan,

    if you solved the problem thanks to an answer, please mark it as verified to encourage the community to provide more and more a better support. Thank you. Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Verified answer
    crm_fan Profile Picture
    95 on at

    Hi All,

    The resolution is weird but this is all i could do to solve this issue.

    I returned the outargument as string (which contained Guid ) and not as entity reference and then passed this string parameter in the 2nd workflow as inargument.

  • RyanCPerry Profile Picture
    105 on at

    If this is registered as a custom action called in a workflow, it is likely that you have specified an output entityreference arguement as required, but your workflow is returning null. Check to see if any of your action output variables are required. If so, set them to optional, save, publish, update plugins and try it then. This fixed the error for me. The problem wasn't the code, it was the action expecting something to be returned.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans