{"record":{"id":"d2d67dce8bec6287","repo":"pentaho/pentaho-kettle","slug":"the-function-call-truncdate-requires-2-arguments-a-date-and-d2d67d","errorCode":null,"errorMessage":"The function call truncDate requires 2 arguments: a date and a level (int)","messagePattern":"The function call truncDate requires 2 arguments: a date and a level \\(int\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":2521,"sourceCode":"        return null;\n      } else if ( isUndefined( ArgList[0] ) ) {\n        return Context.getUndefinedValue();\n      }\n\n      // This is the truncation of a date...\n      // The second argument specifies the level: ms, s, min, hour, day, month, year\n      //\n      Date dArg1 = null;\n      Integer level = null;\n      try {\n        dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );\n        level = (Integer) Context.jsToJava( ArgList[1], Integer.class );\n      } catch ( Exception e ) {\n        throw Context.reportRuntimeError( e.toString() );\n      }\n      return truncDate( dArg1, level );\n    } else {\n      throw Context\n        .reportRuntimeError( \"The function call truncDate requires 2 arguments: a date and a level (int)\" );\n    }\n  }\n\n  @VisibleForTesting\n  static Date truncDate( Date dArg1, Integer level ) {\n    Calendar cal = Calendar.getInstance();\n    cal.setTime( dArg1 );\n\n    switch ( level.intValue() ) {\n      // MONTHS\n      case 5:\n        cal.set( Calendar.MONTH, 0 );\n        // DAYS\n      case 4:\n        cal.set( Calendar.DAY_OF_MONTH, 1 );\n        // HOURS\n      case 3:","sourceCodeStart":2503,"sourceCodeEnd":2539,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L2503-L2539","documentation":"truncDate(date, level) truncates a java.util.Date to a granularity level (0=ms ... 5=month). This error is thrown when the JS call does not pass exactly 2 arguments, i.e. the date and an integer level. It signals an invalid signature, separate from the level range error thrown later inside truncDate.","triggerScenarios":"truncDate() with zero/one argument, truncDate(d) missing the level, truncDate(d, 'MONTH') passing a string level instead of an int, or more than 2 arguments in the JS step.","commonSituations":"Passing a string date like '2024-01-15' plus a string format; using Oracle-style TRUNC(date,'MM') semantics; level supplied as a float like 2.0 from JS math.","solutions":["Pass exactly two arguments: a Date and an integer level, e.g. truncDate(orderDate, 5) (5=month start).","Convert string dates first: truncDate(str2date(s, 'yyyy-MM-dd'), 4).","Ensure the level is a whole number (levels 0-5); use parseInt(level, 10).","Review the source switch: 0=millisecond,1=second,2=minute,3=hour,4=day,5=month."],"exampleFix":"// before\nvar d = truncDate('2024-01-15', 'MM');\n// after\nvar d = truncDate(str2date('2024-01-15', 'yyyy-MM-dd'), 5);","handlingStrategy":"validation","validationCode":"function safeTruncDate(d, lvl) { if (!(d instanceof Date) || lvl == null || lvl < 0 || lvl > 5 || lvl !== Math.floor(lvl)) return null; return truncDate(d, lvl); }","typeGuard":"function isTruncLevel(v) { return typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 5; }","tryCatchPattern":"try { d = truncDate(dt, lvl); } catch (e) { Logger.LogError('truncDate failed: ' + e.message); d = null; }","preventionTips":["Always pass exactly 2 arguments: a Date and an int level 0-5","Convert string dates with str2date before calling","Remember level scale: 0=ms,1=sec,2=min,3=hour,4=day,5=month"],"tags":["javascript","scripting","argument-count","date"],"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"}