{"record":{"id":"b4117e4803e21383","repo":"pentaho/pentaho-kettle","slug":"the-function-call-replace-is-not-valid-wrong-number-of-b4117e","errorCode":null,"errorMessage":"The function call replace is not valid (wrong number of arguments)","messagePattern":"The function call replace is not valid \\(wrong number of arguments\\)","errorType":"exception","errorClass":"RhinoRuntimeError","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1557,"sourceCode":"  }\n\n  public static String replace( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n    try {\n      if ( ArgList.length >= 2 && ( ArgList.length - 1 ) % 2 == 0 ) {\n        if ( isNull( ArgList, new int[] { 0, 1 } ) ) {\n          return null;\n        } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {\n          return (String) Context.getUndefinedValue();\n        }\n        String objForReplace = Context.toString( ArgList[0] );\n        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {\n          objForReplace =\n            objForReplace.replaceAll( Context.toString( ArgList[i] ), Context.toString( ArgList[i + 1] ) );\n        }\n        return objForReplace;\n      } else {\n        throw Context.reportRuntimeError( \"The function call replace is not valid (wrong number of arguments)\" );\n      }\n    } catch ( Exception e ) {\n      throw Context.reportRuntimeError( \"Function call replace is not valid : \" + e.getMessage() );\n    }\n  }\n\n  // Implementation of the JS AlertBox\n  public static String Alert( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n\n    SpoonInterface spoon = SpoonFactory.getInstance();\n    if ( ArgList.length == 1 && spoon != null ) {\n      String strMessage = Context.toString( ArgList[0] );\n      boolean ok = spoon.messageBox( strMessage, \"Alert\", true, Const.INFO );\n      if ( !ok ) {\n        throw new RuntimeException( \"Alert dialog cancelled by user.\" );\n      }\n    }","sourceCodeStart":1539,"sourceCodeEnd":1575,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1539-L1575","documentation":"The ScriptValuesMod `replace(str, search1, repl1, ...)` function requires an odd number of arguments (the source string plus complete search/replacement pairs). It throws this runtime error when `ArgList.length` is even or < 3, i.e. there is no valid search/replace pair.","triggerScenarios":"Calling replace() with 0, 1, 2, 4... arguments, e.g. `replace(str)` or `replace(str, 'a')` (a search value with no replacement).","commonSituations":"Forgetting the replacement string; using regex-style replace() with only a pattern; copy-paste from JavaScript docs where replace takes 2 args but this variant takes 3+ (odd count).","solutions":["Pass an odd number of arguments: replace(source, search1, replacement1[, search2, replacement2, ...])","Ensure every search value has a matching replacement value","For single search/replace, call: replace(field, 'old', 'new')"],"exampleFix":"// before\nvar out = replace(myField, '-');\n// after\nvar out = replace(myField, '-', '_');","handlingStrategy":"validation","validationCode":"if (arguments.length < 3 || arguments.length % 2 === 0) {\n  throw new Error('replace requires (str, search1, repl1[, search2, repl2...])');\n}\nvar out = replace(str, 'a', 'b');","typeGuard":"function isReplaceSafe(args) { return args.length >= 3 && args.length % 2 === 1; }","tryCatchPattern":null,"preventionTips":["Remember: argument count must be odd (source + complete pairs)","Never call replace with a search but no replacement","Prefer plain JavaScript .replace(/regex/g, 'new') for single substitutions"],"tags":["javascript","pentaho","scripting","argument-count"],"backgroundTag":"missing-required-argument","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}