Invalid return statement return

We had a process that has worked for awhile and all of the sudden it starts to produce an error.

I have a Script Task to create a password and it errors now with an Invalid Return Statement.

Can someone see if they can find what we are doing wrong?

ThanksPasswordGenerate.bpmn (3.1 KB)

Here is the js file;

function generatePassword(length, rules) {
if (!length || length == undefined) {
    length = 10;
}

if (!rules || rules == undefined) {
    rules = [
        {chars: "abcdefghijklmnopqrstuvwxyz", min: 3},  // As least 3 lowercase letters
        {chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 3},  // At least 2 uppercase letters
        {chars: "0123456789", min: 2},                  // At least 2 digits
        {chars: "!@#$&*?|%+-_./:;=()[]{}", min: 2}      // At least 1 special char
    ];
}

var allChars = "", allMin = 0;
rules.forEach(function(rule) {
    allChars += rule.chars;
    allMin += rule.min;
});
if (length < allMin) {
    length = allMin;
}
rules.push({chars: allChars, min: length - allMin});

var pswd = "";
rules.forEach(function(rule) {
    if (rule.min > 0) {
        pswd += shuffleString(rule.chars, rule.min);
    }
});

return shuffleString(pswd);
}

function shuffleString(str, maxlength) {
var shuffledString = str.split('').sort(function(){return 0.5-Math.random()}).join('');
if (maxlength > 0) {
    shuffledString = shuffledString.substr(0, maxlength);
}
return shuffledString;
}

var pswd = generatePassword(15, [
  {chars: "abcdefghijklmnopqrstuvwxyz", min: 4},  // As least 4 lowercase letters
  {chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 1},  // At least 1 uppercase letters
  {chars: "0123456789", min: 3},                  // At least 3 digits
  {chars: "!@#$&*?|%+-_./:;=()[]{}", min: 2}      // At least 2 special chars
]);

return generatePassword();

Can you give more details about this error?
Maybe show the full stack trace?

1 Like

Yes @Niall Thanks for your help.

I have separated out the script into this simplified BPMN file with just 2 tasks.

  1. Service Task with the Password Generator JS file
  2. User Task to stop Execution

When I try to start this process manually in the Tasklist, this is the message I get;

The process could not be started. :
Cannot instantiate process definition PasswordTest Unable to evaluate script while executing activity 'Activity_0dasfj' in the process definition with id 'PasswordTest:eb-bb95-0242ac170002':<eval>:50:0 Invalid return statement
return generatePassword();
^ in <eval> at line number 50 at column number 0

I’m at a loss since I’m pretty sure this ran great for weeks without a problem.

Here is the full trace stack from the original Process;

org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script while executing activity 'Activity_0yk8nmj' in the process definition with id 'user_setup:30:45-024170002':<eval>:50:0 Invalid return statement

return generatePassword();
^ in at line number 50 at column number 0
at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluate(SourceExecutableScript.java:71)
at org.camunda.bpm.engine.impl.scripting.ResourceExecutableScript.evaluate(ResourceExecutableScript.java:46)
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.behavior.ScriptTaskActivityBehavior$1.call(ScriptTaskActivityBehavior.java:55)
at org.camunda.bpm.engine.impl.bpmn.behavior.ScriptTaskActivityBehavior$1.call(ScriptTaskActivityBehavior.java:51)

@Ashley remove return keyword from the last line of the code. It works fine. Should be generatePassword(); .

function generatePassword(length, rules) {
if (!length || length == undefined) {
    length = 10;
}

if (!rules || rules == undefined) {
    rules = [
        {chars: "abcdefghijklmnopqrstuvwxyz", min: 3},  // As least 3 lowercase letters
        {chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 3},  // At least 2 uppercase letters
        {chars: "0123456789", min: 2},                  // At least 2 digits
        {chars: "!@#$&*?|%+-_./:;=()[]{}", min: 2}      // At least 1 special char
    ];
}

var allChars = "", allMin = 0;
rules.forEach(function(rule) {
    allChars += rule.chars;
    allMin += rule.min;
});
if (length < allMin) {
    length = allMin;
}
rules.push({chars: allChars, min: length - allMin});

var pswd = "";
rules.forEach(function(rule) {
    if (rule.min > 0) {
        pswd += shuffleString(rule.chars, rule.min);
    }
});

return shuffleString(pswd);
}

function shuffleString(str, maxlength) {
var shuffledString = str.split('').sort(function(){return 0.5-Math.random()}).join('');
if (maxlength > 0) {
    shuffledString = shuffledString.substr(0, maxlength);
}
return shuffledString;
}

var pswd = generatePassword(15, [
  {chars: "abcdefghijklmnopqrstuvwxyz", min: 4},  // As least 4 lowercase letters
  {chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 1},  // At least 1 uppercase letters
  {chars: "0123456789", min: 3},                  // At least 3 digits
  {chars: "!@#$&*?|%+-_./:;=()[]{}", min: 2}      // At least 2 special chars
]);

generatePassword();
1 Like

@aravindhrs @Niall You both are gentleman and scholars :slight_smile:

Removing the return solved the problem!

Thank you

2 Likes

@Ashley Glad it worked :slight_smile: Welcome to the forum :tada: