cancel
Showing results for 
Search instead for 
Did you mean: 

Start another workflow from a service task of a workflow

stondini
Champ in-the-making
Champ in-the-making
Hello,

I'd like to start a workflow from another workflow without using <em>callActivity</em> element because I'd the first workflow ends.
If I use <em>callActivity</em> element, the first workflow will wait the end of the second one.

I need two independent workflows.

Here, the service task of the first workflow :

    <serviceTask id="incomingClientDocument" name="Client Workflow" activiti:class="org.alfresco.repo.workflow.activiti.script.AlfrescoScriptDelegate">
      <extensionElements>
        <activiti:field name="script">
          <activiti:string><![CDATA[
                                                logger.log('incomingClientDocument-START');
                  var clientNodeRef = execution.getVariable('rtcm_clientAssoc');
                  var dueDate = execution.getVariable('bpm_workflowDueDate');
                                                // Here, the second workflow starts…
                  rtTaskService.startIncomingClientDocument(clientNodeRef, bpm_package, dueDate);
                  logger.log('incomingClientDocument-END');
           ]]></activiti:string>
        </activiti:field>
      </extensionElements>
    </serviceTask>


And here the Java code of <em>rtTaskService.startIncomingClientDocument()</em>

  public void startIncomingClientDocument(final ScriptNode clientNode, final ScriptNode packageNode, final Date dueDate) {

    logger.info("Start Incoming Client Document Workflow");

    NodeService nodeService = this.getServiceRegistry().getNodeService();
    WorkflowService workflowService = this.getServiceRegistry().getWorkflowService();

    ClientEntity clientEntity = new ClientEntity(getServiceRegistry(), clientNode.getNodeRef());

    // Create a package that contains the items of the source package
    NodeRef packageNodeRef = workflowService.createPackage(null);
    if (packageNode != null) {
      for (ChildAssociationRef childRef : nodeService.getChildAssocs(packageNode.getNodeRef()))
        nodeService.addChild(packageNodeRef, childRef.getChildRef(), WorkflowModel.ASSOC_PACKAGE_CONTAINS,
            QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, "incomingClientDocument"));
    }

    WorkflowDefinition workflowDefinition = workflowService.getDefinitionByName("activiti$IncomingClientDocument");
    Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
    parameters.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Incoming Client Document");
    parameters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dueDate);
    parameters.put(WorkflowModel.PROP_DUE_DATE, dueDate);
    parameters.put(WorkflowModel.ASSOC_GROUP_ASSIGNEE, clientEntity.getGroupAuthorityName());
    parameters.put(WorkflowModel.ASSOC_PACKAGE, packageNodeRef);
    parameters.put(ClientRelatedContentModel.ASSOC_CLIENT, clientNode.getNodeRef());
    workflowService.startWorkflow(workflowDefinition.getId(), parameters);
  }


When I execute the first workflow, it raises the following error :

Error while completing sub process of execution ProcessInstance[92626]


Do you have an idea of what is wrong ?

Thank you.
1 REPLY 1

lementree
Champ on-the-rise
Champ on-the-rise
Hi,

You need add few more lines to your Java code after start workflow. As below


WorkflowPath path = workflowService.startWorkflow(workflowDefinition.getId(), parameters);
String wfPathId = path.getId();
List<WorkflowTask> wfTasks = workflowService.getTasksForWorkflowPath(wfPathId);
if (wfTasks.size() == 0)return;
WorkflowTask wfStartTask = wfTasks.get(0);
workflowService.endTask(wfStartTask.getId(), null);