pentaho/pentaho-kettle · error · JavaScriptException

The function call year requires 1 argument.

Error message

The function call year requires 1 argument.

What it means

The year() JavaScript function extracts the calendar year from a Date argument and returns it as a number. When it is not called with exactly one argument, it throws this runtime error before any date conversion happens.

Solutions

  1. Call year() with exactly one argument.
  2. Pass a java.util.Date (use str2Date() first if you have a string).
  3. Null-check the date field before calling.
  4. Convert string dates explicitly: year(str2Date(dateStr, 'yyyy-MM-dd')).

Example fix

// before
var y = year();
// after
var y = year(myDateField); // myDateField is a Date from str2Date()
Defensive patterns

Strategy: validation

Validate before calling

if (myDateField == null) throw 'year: date is null';
var y = year(myDateField);

Type guard

function isDate(v) { return v != null && v.getClass && String(v.getClass()) === 'class java.util.Date'; }

Try / catch

try { var y = year(myDateField); } catch (e) { var y = null; }

Prevention

When it happens

Trigger: Calling year() in a modified JavaScript step with zero arguments, or with two or more arguments; also when the argument is null the arity check (length==1) may pass but the cast then fails with a different wrapped message.

Common situations: Scripts ported from SQL YEAR() semantics with different argument expectations, forgetting to pass the date field, or passing a date string instead of a Date object.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/23de1928d6fbf50a. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java:871

    }
    return null;
  }

  public static Object year( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {
    try {
      if ( ArgList.length == 1 ) {
        if ( isNull( ArgList[0] ) ) {
          return new Double( Double.NaN );
        } else if ( isUndefined( ArgList[0] ) ) {
          return Context.getUndefinedValue();
        }
        java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
        Calendar cal = Calendar.getInstance();
        cal.setTime( dArg1 );
        return new Double( cal.get( Calendar.YEAR ) );
      } else {
        throw Context.reportRuntimeError( "The function call year requires 1 argument." );
      }
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  }

  public static Object month( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {
    try {
      if ( ArgList.length == 1 ) {
        if ( isNull( ArgList[0] ) ) {
          return new Double( Double.NaN );
        } else if ( isUndefined( ArgList[0] ) ) {
          return Context.getUndefinedValue();
        }
        java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
        Calendar cal = Calendar.getInstance();
        cal.setTime( dArg1 );

View on GitHub (pinned to f3058517a1)