How to change who started a process?

cancel
Showing results for 
Search instead for 
Did you mean: 
james_kleeh
Active Member

How to change who started a process?

Currently when I'm starting a process, I set the authenticatedUserId through the identity service. I would like to change who started a project (transfer ownership functionality). I have changed the identity links, however when querying process instances with `.startedBy()`, it still shows the old user as having started the process. Any help would be greatly appreciated.

Example:

historyService
        .createHistoricProcessInstanceQuery()
        .startedBy("OLDUSER")
        .unfinished()
        .list()

Thanks!

5 Replies
daisuke-yoshimo
Senior Member

Re: How to change who started a process?

Perhaps,there is no public API for your use case.

You need to use dao directly as follows.

Example:

Context.getCommandContext()
.getHistoricProcessInstanceEntityManager()
.findHistoricProcessInstance(%ProcessInstanceId%)
.setStartUserId(%UserId%);

james_kleeh
Active Member

Re: How to change who started a process?

In my case Context.getCommandContext() is returning null. Any idea why?

I tried 

processEngineConfiguration.commandExecutor.execute(new Command<Object>() {
    Object execute(CommandContext commandContext) {
        commandContext
            .getHistoricProcessInstanceEntityManager()
            .findHistoricProcessInstance(id)
            .setStartUserId(username)
    }
})

and it doesn't fail, however the change doesn't take effect

I've also tried the following

processEngineConfiguration.commandExecutor.execute(new Command<HistoricProcessInstance>() {
    HistoricProcessInstance execute(CommandContext commandContext) {
        HistoricProcessInstanceEntityManager manager = commandContext.getHistoricProcessInstanceEntityManager()
        HistoricProcessInstance instance = manager.findHistoricProcessInstance(id)
        instance.setStartUserId(username)
        commandContext.dbSqlSession.update(instance)
        instance
    }
})
james_kleeh
Active Member

Re: How to change who started a process?

Looks like what I'm trying to do is not possible:

Activiti/HistoricProcessInstance.xml at master · Activiti/Activiti · GitHub 

The update statement gets executed, but start_user_id is not part of the fields that get set

Fjordo
Active Member II

Re: How to change who started a process?

Have you found an answer? I have the same issue.

thanks

Fjordo
Active Member II

Re: How to change who started a process?

This worked correctly to me, I made an execution listener on the "start" of the Start Task and here is the code

 

public class SetAuthorAsWFInitiator implements ExecutionListener {

	private static final long serialVersionUID = 1095132995665073418L;

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Object>() {

			@Override
			public Object doWork() throws Exception {
				
				ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
				Map<Object, Object> registeredBeans = processEngineConfiguration.getBeans();
				ServiceRegistry serviceRegistry = (ServiceRegistry) registeredBeans.get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
				NodeService nodeService = serviceRegistry.getNodeService();
				ActivitiScriptNode bpm_package = (ActivitiScriptNode) execution.getVariable("bpm_package");
				List<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(bpm_package.getNodeRef());
				NodeRef documentNode = childAssocs.get(0).getChildRef();
				
				List<AssociationRef> targetAssocs = nodeService.getTargetAssocs(documentNode, Constants.AUTHOR_ASSOC);
				NodeRef creatorNodeRef = targetAssocs.get(0).getTargetRef();
				NodeRef creatorHomeFolderNodeRef = (NodeRef) nodeService.getProperty(creatorNodeRef, ContentModel.PROP_HOMEFOLDER);

				execution.setVariable("initiator", new ActivitiScriptNode(creatorNodeRef, serviceRegistry));
				execution.setVariable("initiatorhome", new ActivitiScriptNode(creatorHomeFolderNodeRef, serviceRegistry));
				
				processEngineConfiguration.getCommandExecutor().execute(new Command<Object>() {
				    public Object execute(CommandContext commandContext) {
				        commandContext
				            .getHistoricProcessInstanceEntityManager()
				            .findHistoricProcessInstance(execution.getProcessInstanceId())
				            .setStartUserId((String) nodeService.getProperty(creatorNodeRef, ContentModel.PROP_USERNAME));
						return commandContext;
				    }
				});
				
				
				return null;
			}

		}, AuthenticationUtil.getAdminUserName());
	}
}