Access LoopCounter var in Multi Instance DMN

I have a single multi-instance DMN decision task (MI could be both parallel and sequantial) where for each iteration I want to get the “loopCounter” variable mentioned in the docs. In all my efforts I failed to access this variable.

How should I position the script to access it? I tried execution listeners on both start and end of the MI DMN task and both var counter = execution.getVariableLocal(“loopCounter”) and var counter = execution.getVariable(“loopCounter”). Unfortunately, neither is working. In all I get the following error in the Cockpit:

org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script while executing activity ‘Activity_0h49bxx’ in the process definition with id ‘multiInstanceDMNSandbox:12:e50adca6-a989-11eb-93e4-0242ac110002’::7:41 Expected an operand but found error
var counter = execution.getVariableLocal(“loopCounter”);
^ in at line number 7 at column number 41
at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluate(SourceExecutableScript.java:71)
at org.camunda.bpm.engine.impl.scripting.ExecutableScript.execute(ExecutableScript.java:63)
at org.camunda.bpm.engine.impl.scripting.env.ScriptingEnvironment.execute(ScriptingEnvironment.java:101)
at org.camunda.bpm.engine.impl.scripting.env.ScriptingEnvironment.execute(ScriptingEnvironment.java:87)
at org.camunda.bpm.engine.impl.delegate.ScriptInvocation.invoke(ScriptInvocation.java:47)
at org.camunda.bpm.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:58)
at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocationInContext(DefaultDelegateInterceptor.java:92)
at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:63)
at org.camunda.bpm.engine.impl.bpmn.listener.ScriptExecutionListener.notify(ScriptExecutionListener.java:43)
at org.camunda.bpm.engine.impl.bpmn.listener.ScriptExecutionListener.notify(ScriptExecutionListener.java:30)
at org.camunda.bpm.engine.impl.core.instance.CoreExecution.invokeListener(CoreExecution.java:101)
at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:55)
at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:99)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:131)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:111)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:86)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:76)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:643)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:618)
at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:62)
at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:99)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:131)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:111)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:86)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:634)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:608)
at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationActivityStartCreateScope.scopeCreated(PvmAtomicOperationActivityStartCreateScope.java:43)
at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:54)
at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:28)
at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:99)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:131)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:118)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:86)
at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:76)
at org.camunda.bpm.engine.impl.jobexecutor.AsyncContinuationJobHandler.execute(AsyncContinuationJobHandler.java:81)
at org.camunda.bpm.engine.impl.jobexecutor.AsyncContinuationJobHandler.execute(AsyncContinuationJobHandler.java:40)
at org.camunda.bpm.engine.impl.persistence.entity.JobEntity.execute(JobEntity.java:134)
at org.camunda.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:110)
at org.camunda.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:43)
at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:28)
at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:110)
at org.camunda.bpm.engine.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:72)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
at org.camunda.bpm.engine.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:70)
at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:70)
at org.camunda.bpm.engine.impl.interceptor.CommandCounterInterceptor.execute(CommandCounterInterceptor.java:35)
at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:57)
at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.executeJob(ExecuteJobsRunnable.java:110)
at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:71)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javax.script.ScriptException: :7:41 Expected an operand but found error
var counter = execution.getVariableLocal(“loopCounter”);
^ in at line number 7 at column number 41
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:544)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:531)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:409)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:162)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluateScript(SourceExecutableScript.java:125)
at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluate(SourceExecutableScript.java:65)
… 52 more
Caused by: jdk.nashorn.internal.runtime.ParserException: :7:41 Expected an operand but found error
var counter = execution.getVariableLocal(“loopCounter”);
^
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:297)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:282)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:4462)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4620)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.conditionalExpression(Parser.java:4772)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.assignmentExpression(Parser.java:4711)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.argumentList(Parser.java:3728)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:3410)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:4440)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4620)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.conditionalExpression(Parser.java:4772)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.assignmentExpression(Parser.java:4711)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.variableDeclarationList(Parser.java:1633)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.variableStatement(Parser.java:1532)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.statement(Parser.java:1042)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:909)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.program(Parser.java:844)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:325)
at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:285)
at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compile(Context.java:1500)
at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1467)
at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compileScript(Context.java:750)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:542)
… 58 more

Hi @IdemenB,

do you want to access the loop counter in the decision or in the execution listener that is attached to the business rule task?

How do you want to use the loop counter?

In an execution listener, the loop counter can be access by its name. For example,

print(execution.getVariable("loopCounter"))

Best regards,
Philipp

Thank you very much @Philipp_Ossler , I’ll try once again after your screenshot.

My purpose was to use this to be able to create multi-instance tasks withouts overwriting some process variables by adding “xxx_loopCounterIndex” at the end of the variable name.