How do I determine whether the process instance type to which a task node belongs is a master process or a subprocess?

camunda version :7.14.0
I have a business scenario where I want to do different jump tests when the node is a main process task or a sub-process task.So, my question is, when the process runs to the 02-01-node, how do I tell if it is an embedded sub-process node or a main process node?What I want is a way to judge from the source code.If my above expression is not clear, I can raise your question.

camunda-044.bpmn (6.1 KB)

Hello @top-polarbear, a one-liner approach that could maybe help is looking at the parent of the Task object that you are currently on, and checking whether the parent type is a process or a subprocess.

String parentElementTypeName = task.getParentElement().getElementType().getTypeName();
if (parentElementTypeName == "process") { ... }

I’m sure there are other (probably better too) ways of finding this out, but for the context of your question I hope this helps :slight_smile:

1 Like

Thank you very much for your answer, but I don’t think it meets all the circumstances.I wrote an example. You can also make a suggestion.

/**
	 * @MethodName: querySubProcessByTaskId
	 * @Description: Query the process type information of which it belongs according to taskId
	 * 								(The process types include: main process(process), embedded sub-process(subProcess),  external sub-process(callActivity))
	 * @return void
	 * @author top-polarbear
	 * @date 2021-2-4 16:26:34
	 * @version v1.0
	 */
	@Test
	public void queryProcessTypeByTaskId() {
		String taskId = "b7304587-66c0-11eb-9b55-d8c497903c0e";
		HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
		if(!StringUtils.equals(historicTaskInstance.getRootProcessInstanceId(), historicTaskInstance.getProcessInstanceId())) {
	    	log.info("processID:{};ProcessType: external sub-process。",historicTaskInstance.getProcessInstanceId());
	    } else  {
	    	ActivityInstance rootActivityInstance = runtimeService.getActivityInstance(historicTaskInstance.getProcessInstanceId());
			Map<String, Object> processTypeData = judgeProcessType(rootActivityInstance, rootActivityInstance.getActivityId(), historicTaskInstance.getTaskDefinitionKey(),false, false);
	  	     if ((boolean) processTypeData.get("isSubProcess")) {
	    	 log.info("processID:{};ProcessType: embedded sub-process。",historicTaskInstance.getProcessInstanceId());
			}else if (!(boolean) processTypeData.get("isSubProcess")) {
				log.info("processID:{};ProcessType: main process。",historicTaskInstance.getProcessInstanceId());
			}
	    }
	}
	
	/**
	 * @MethodName: judgeProcessType
	 * @Description: return process type
	 * @param  activityInstance:The process defines the activity instance
	 * @param  processActivityId:
	 * @param  taskDefinitionKey:The task definition ID of the designer
	 * @param isSubProcess
	 * @param  isMultiInstanceBody 
	 * @return Map<String,Object> return type
	 * @author top-polarbear
	 * @date 2021-2-4 16:32:37
	 * @version v1.0
	 */
	private Map<String, Object> judgeProcessType(ActivityInstance activityInstance, String  processActivityId, String taskDefinitionKey,  boolean isSubProcess, boolean isMultiInstanceBody) {
		Map<String, Object> processTypeData = new HashMap<>();
		 if (isSubProcess && !isMultiInstanceBody) {
			 processActivityId = activityInstance.getActivityId();
		}
		for (ActivityInstance item : activityInstance.getChildActivityInstances()) { // Iterate the child nodes of the process definition
			if (StringUtils.equals(item.getActivityType(), "userTask")) {
				if (StringUtils.equals(item.getActivityId(), taskDefinitionKey)) {
					if(!isSubProcess) { // User task for the main-process
						processTypeData.put("processActivityId", processActivityId);
						processTypeData.put("processType", "mainProcess");
						processTypeData.put("isSubProcess", isSubProcess);
						 return processTypeData;
					} else { // User task for the sub-process
						processTypeData.put("processActivityId", processActivityId);
						processTypeData.put("processType","subProcess");
						processTypeData.put("isSubProcess", isSubProcess);
						 return processTypeData;
					}
				}
			}
			if (StringUtils.equals(item.getActivityType(), "multiInstanceBody")) { // Multi-instance task
				processTypeData =  judgeProcessType(item, processActivityId, taskDefinitionKey, isSubProcess, true);
				if (processTypeData.size() > 0) {
					return processTypeData;
				}
			}
			if (StringUtils.equals(item.getActivityType(), "subProcess")) { // sub-process
				processTypeData = 	judgeProcessType(item, processActivityId, taskDefinitionKey, true, false);
				if (processTypeData.size() > 0) {
					return processTypeData;
				}
			}
		}
		return processTypeData;
	}

camunda-046.bpmn (21.2 KB)