05-08-2017 03:26 PM
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!
05-09-2017 12:20 AM
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%);
05-09-2017 02:53 PM
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
}
})
05-09-2017 04:08 PM
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
07-19-2021 06:34 AM
Have you found an answer? I have the same issue.
thanks
07-19-2021 06:47 AM
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());
}
}
Explore our Alfresco products with the links below. Use labels to filter content by product module.