Custom workflow is not running in Dynamics CRM 2016

616 Views Asked by At

I've a custom workflow on order form that needed to remove followship of the order if the order is no more followed by a user. The custom workflow works fine when I set it to run on-demand but doesn't work when I uncheck the on-demand option in Dynamics CRM 2016.

Following is my code snippet and the details that I received through tracing service. Can anyone please guide on this?

Code Snippet

Guid orderId = this.inputEntity.Get(caContext).Id;

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
               "  <entity name='postfollow'>" +
               "    <attribute name='regardingobjectid' />" +
               "    <attribute name='ownerid' />" +
               "    <filter type='and'>" +
               "      <condition attribute='regardingobjectid' operator='eq' uitype='salesorder' value='"+ orderId + "' />" +
               "    </filter>" +
               "  </entity>" +
               "</fetch>";
var followQuery = new FetchExpression(fetchXml);
EntityCollection followCollection = service.RetrieveMultiple(followQuery);
temp = followCollection.Entities.Count;
if (temp < 1)
{
    return;
}
ownerIds = new string[temp];
foreach (Entity follow in followCollection.Entities)
{
    ownerIds[i] = (((EntityReference)follow.Attributes["ownerid"]).Id).ToString();
    i++;
}

List<string> allUsersList = allUsers(service, orderId);
string[] allusersArray = allUsersList.Distinct().ToArray();
do
{
    if (allusersArray.Contains(ownerIds[j].ToLower()))
    {

    }
    else
    {
        removeFollowShip(caContext, service, new Guid(ownerIds[j]), orderId);
    }
    j++;
} while (j < ownerIds.Length - 1);

Tracing Service Details

Plugin Trace:

[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
[RetrieveEntity]

Error Message:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Entity Reference cannot have Id and Key Attributes empty.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220989</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Entity Reference cannot have Id and Key Attributes empty.</Message>
  <Timestamp>2017-04-11T16:17:32.745259Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>System.ArgumentException: Entity Reference cannot have Id and Key Attributes empty.</Message>
    <Timestamp>2017-04-11T16:17:32.745259Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText>[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
[RetrieveEntity]
</TraceText>
</OrganizationServiceFault>
   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(EntityReference entityReference, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)
   at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.<>c__DisplayClass1.<RetrieveInternal>b__0(IOrganizationService sdkService)
   at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.ExecuteInternal(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.Execute(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
3

There are 3 best solutions below

0
Josh Painter On

When a workflow runs on-demand, it runs under the security context of the user who ran it. When it runs in the background, it runs under the security context of the owner of the workflow. Do you own the workflow? If not, try assigning it to yourself and see if it works as a background workflow then.

0
Dave Clark On

Try replacing:

(((EntityReference)follow.Attributes["ownerid"]).Id).ToString();

with:

follow.Attributes.Contains("ownerid") ? (follow.GetAttributeValue<EntityReference>("ownerid").Id).ToString() : "";
0
Greg Kawinski On

We were getting this error also (Entity Reference cannot have Id and Key Attributes empty). I found registering the custom workflow assembly in Sandbox isolation mode resolved it (in the plug-in registration tool).