{"record":{"id":"c62ec06d145e984d","repo":"pentaho/pentaho-kettle","slug":"the-function-call-indexof-requires-2-or-3-arguments-c62ec0","errorCode":null,"errorMessage":"The function call indexOf requires 2 or 3 arguments","messagePattern":"The function call indexOf requires 2 or 3 arguments","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":195,"sourceCode":"\n  public static int indexOf( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n\n    int returnIndex = -1;\n\n    if ( ArgList.length == 2 || ArgList.length == 3 ) {\n      if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {\n        String string = Context.toString( ArgList[0] );\n        String subString = Context.toString( ArgList[1] );\n\n        int fromIndex = 0;\n        if ( ArgList.length == 3 ) {\n          fromIndex = (int) Math.round( Context.toNumber( ArgList[2] ) );\n        }\n        returnIndex = string.indexOf( subString, fromIndex );\n      }\n    } else {\n      throw Context.reportRuntimeError( \"The function call indexOf requires 2 or 3 arguments\" );\n    }\n    return returnIndex;\n  }\n\n  public static Object getTransformationName( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n    try {\n      Object objTranName = Context.toString( actualObject.get( \"_TransformationName_\", actualObject ) );\n      return objTranName;\n    } catch ( Exception e ) {\n      throw Context.reportRuntimeError( e.toString() );\n    }\n  }\n\n  public static void appendToFile( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n\n    if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L177-L213","documentation":"indexOf is a custom Rhino string helper that finds a substring and requires exactly 2 or 3 arguments (string, substring, optional fromIndex). Any other count throws the Rhino RuntimeError 'The function call indexOf requires 2 or 3 arguments'.","triggerScenarios":"Calling indexOf with only one argument (JS-style str.indexOf(sub) confusion — this helper takes the string as the first argument), or with more than 3 arguments.","commonSituations":"Using native JavaScript indexOf syntax (value.indexOf(sub)) with this helper instead; passing extra flags as a 4th argument; forgetting the substring argument.","solutions":["Call indexOf(str, subString) with the target string first.","Add the optional third fromIndex argument only as a number.","Remove any arguments beyond the third."],"exampleFix":"// before\nvar i = value.indexOf(sub); // wrong: only 1 arg to helper\n// after\nvar i = indexOf(value, sub);","handlingStrategy":"type-guard","validationCode":"// JS guard before calling\nif (str === null || str === undefined || sub === undefined) return -1;\nvar i = indexOf(String(str), String(sub));","typeGuard":"function safeIndexOf(str, sub, fromIndex) {\n  if (str === null || str === undefined || sub === undefined) return -1;\n  return (fromIndex === undefined) ? indexOf(String(str), String(sub))\n                                   : indexOf(String(str), String(sub), Number(fromIndex));\n}","tryCatchPattern":"try { var i = indexOf(s, sub); }\ncatch (e) { if (String(e).indexOf('2 or 3 arguments') >= 0) { /* fix arity */ } throw e; }","preventionTips":["Remember this helper takes (string, substring) unlike native indexOf","Don't pass more than the optional third argument","Coerce inputs to strings","Test the script in Spoon before saving the transformation"],"tags":["javascript","rhino","argument-count","pdi"],"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"}