locked
Custom Workflow: Entity mainEntity = (Entity)context.Context.InputParameters[WorkflowCommon.TARGET] RRS feed

  • Question

  • Dear Reader,

    i have a Problem with executing a workflow on demand : WorkflowCommon.TARGET is not present in the dictionary

    InputParameters.

    If i use the workflow triggered by create, fields changed, etc. it works great.

    The Code:

    public override void Process(WorkflowContext context)
    
    {
    
    Entity mainEntity = (Entity)context.Context.InputParameters[WorkflowCommon.TARGET];
    
    String entityRelationshipName = Relationship.Get<String>(context.ExecutionContext);
    
    Entity subEntity;
    
    String subEntityFieldName;
    
     RetrieveRelationship(context.Service, entityRelationshipName, mainEntity, out subEntity, out subEntityFieldName);
    
    EntityCollection subEntities = RetrieveSubEntities(context.Service, mainEntity, subEntity, subEntityFieldName);
    
    Count.Set(context.ExecutionContext, subEntities.Entities.Count());
    
    }
    


    AND:

    public WorkflowContext(CodeActivityContext executionContext, String activityName)
    
    
    
    {
    
    
    
    ExecutionContext = executionContext;
    
    
    
    ActivityName = activityName;
    
    
    
    LogFactory = new ChannelFactory<IAssLogConnection>(new BasicHttpBinding(), new EndpointAddress(WorkflowCommon.LOG_ENDPOINT_ADDRESS));
    
    
    
    if (LogFactory == null) throw new InvalidPluginExecutionException(ErrorMessage.FAILED_TO_RETRIEVE_LOG_CONNECTION_SERVICE);
    
    
    
    LogConnectionService = LogFactory.CreateChannel();
    
    
    
    if (LogConnectionService == null) throw new InvalidPluginExecutionException(ErrorMessage.FAILED_TO_RETRIEVE_LOG_CONNECTION_SERVICE);
    
    
    
    if (ExecutionContext == null) throw new InvalidPluginExecutionException(ErrorMessage.FAILED_TO_RETRIEVE_EXECUTION_CONTEXT);
    
    
    
     TracingService = ExecutionContext.GetExtension<ITracingService>();
    
    
    
    if (TracingService == null) throw new InvalidPluginExecutionException(ErrorMessage.FAILED_TO_RETRIEVE_TRACING_SERVICE);
    
    
    
     TracingService.Trace(ErrorMessage.ENTERED_EXECUTE_FORMAT_3,
    
    
    
    ActivityName,
    
    
    
    ExecutionContext.ActivityInstanceId,
    
    
    
    ExecutionContext.WorkflowInstanceId);
    
    
    
     Context = ExecutionContext.GetExtension<IWorkflowContext>();
    
    
    
    if (Context == null) throw new InvalidPluginExecutionException(ErrorMessage.FAILED_TO_RETRIEVE_WORKFLOW_CONTEXT);
    
    
    
     TracingService.Trace(ErrorMessage.INITIATING_USER_FORMAT_3,
    
    
    
    ActivityName,
    
    
    
    Context.CorrelationId,
    
    
    
    Context.InitiatingUserId);
    
    
    
     ServiceFactory = ExecutionContext.GetExtension<IOrganizationServiceFactory>();
    
    
    
    Service = ServiceFactory.CreateOrganizationService(Context.UserId);
    
    
    
    }


    Does anyone know, why?

    Thursday, December 3, 2015 8:35 AM

Answers

  • I have got a solution:

            private static Entity RetrieveTargetEntity(WorkflowContext context)
            {
                return context.Service.Retrieve(context.Context.PrimaryEntityName, context.Context.PrimaryEntityId, new ColumnSet(true));
            }
    
    

    • Marked as answer by UlrG Friday, December 4, 2015 9:55 AM
    Friday, December 4, 2015 9:54 AM

All replies

  • The error is almost certainly because WorkflowCommon.TARGET is not present in the InputParameters. You should use context.PrimaryEntityName instead

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, December 3, 2015 12:20 PM
    Moderator
  • I have got a solution:

            private static Entity RetrieveTargetEntity(WorkflowContext context)
            {
                return context.Service.Retrieve(context.Context.PrimaryEntityName, context.Context.PrimaryEntityId, new ColumnSet(true));
            }
    
    

    • Marked as answer by UlrG Friday, December 4, 2015 9:55 AM
    Friday, December 4, 2015 9:54 AM