{"record":{"id":"0d97b900d64dad49","repo":"pentaho/pentaho-kettle","slug":"can-t-set-variable","errorCode":null,"errorMessage":"Can't set variable [","messagePattern":"Can't set variable \\[","errorType":"exception","errorClass":"KettleStepException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/setvariable/SetVariable.java","lineNumber":183,"sourceCode":"      case SetVariableMeta.VARIABLE_TYPE_ROOT_JOB:\n        // Comments by SB\n        // VariableSpace rootJob = null;\n        parentJob = trans.getParentJob();\n        while ( parentJob != null ) {\n          parentJob.setVariable( varname, value );\n          // rootJob = parentJob;\n          parentJob = parentJob.getParentJob();\n        }\n        break;\n\n      case SetVariableMeta.VARIABLE_TYPE_GRAND_PARENT_JOB:\n        // Set the variable in the parent job\n        //\n        parentJob = trans.getParentJob();\n        if ( parentJob != null ) {\n          parentJob.setVariable( varname, value );\n        } else {\n          throw new KettleStepException( \"Can't set variable [\"\n            + varname + \"] on parent job: the parent job is not available\" );\n        }\n\n        // Set the variable on the grand-parent job\n        //\n        VariableSpace gpJob = trans.getParentJob().getParentJob();\n        if ( gpJob != null ) {\n          gpJob.setVariable( varname, value );\n        } else {\n          throw new KettleStepException( \"Can't set variable [\"\n            + varname + \"] on grand parent job: the grand parent job is not available\" );\n        }\n        break;\n\n      case SetVariableMeta.VARIABLE_TYPE_PARENT_JOB:\n        // Set the variable in the parent job\n        //\n        parentJob = trans.getParentJob();","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/setvariable/SetVariable.java#L165-L201","documentation":"When the variable scope is 'set on the parent job' (VARIABLE_TYPE_PARENT_JOB... parent-job variant), the step asks the transformation's parent job to store the variable. If trans.getParentJob() returns null — the transformation is not running inside a job — the variable cannot be set and a KettleStepException is thrown.","triggerScenarios":"Step variable type is PARENT_JOB (or GRAND_PARENT_JOB variant path) and processRow calls setValue while trans.getParentJob() == null, i.e. the transformation runs standalone or via a caller that is not a Job.","commonSituations":"Running a transformation directly in Spoon or via Kitchen/trans without a wrapping job; switching a Set Variable step's scope from 'Valid in the root job' to 'Valid in the parent job' while it is not executed from a job.","solutions":["Run the transformation inside a Job (e.g. a Job entry 'Transformation') so a parent job exists","Change the variable scope to 'Valid in the Java virtual machine' or 'Valid in the root job' which does not require a parent job","If executed via API, wrap the Trans in an executing Job or set the variable directly on your own VariableSpace"],"exampleFix":"// before\ntrans.execute(null);\n// after\nJob job = new Job(null, jobMeta);\njob.getJobMeta().addJobEntry(...transformation entry...);\njob.start();","handlingStrategy":"validation","validationCode":"if (trans.getParentJob() == null) {\n  // transformation is running standalone: avoid PARENT_JOB scope\n  setVariableScope(SetVariableMeta.VARIABLE_TYPE_ROOT_JOB);\n}","typeGuard":"boolean hasParentJob(Trans trans) { return trans != null && trans.getParentJob() != null; }","tryCatchPattern":"try { trans.execute(null); } catch (KettleStepException e) { if (e.getMessage().startsWith(\"Can't set variable\")) { /* fallback: JVM scope or wrap in a Job */ } else throw e; }","preventionTips":["Run transformations that set variables from within a Job","Use 'Valid in the Java virtual machine' scope when executing standalone","Document scope requirements per transformation"],"tags":["kettle","pdi","set-variable","scope","parent-job"],"backgroundTag":"invalid-state-transition","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"}