Using RedwoodScript in Jobs 
RedwoodScript is the ideal language to automate complex tasks, adding custom code to a job chain, for example. The following code will illustrate some example features you would like to implement. The easiest way to develop your code is to use the web-based shell first. If it runs in there, it should run in a job definition, please see the RedwoodScript, Redwood Expression Language, and Scripting Contexts sections for a list of examples, functions, contexts and pre-defined objects, respectively. Please note that the web-based shell does not have parent jobs, so parent/child examples will not run in it.
Note
Redwood Software does not warrant that this is the most effective way in solving your specific problem. The examples are provided to illustrate the use of RedwoodScript.
To access the current job parameters, remember the current job is already defined as jcsJob and the SchedulerSession is defined as jcsSession.
Parameters are predefined as parameters.<parameterName> for the In value and outParameter.<parameterName> for the Out value.
{
jcsOut.println(Name);
jcsOut.println.println(Address);
jcsOut.println.println(City);
jcsOut.println.println(Zip);
}
You can also use the job to retrieve the parameters and then their values. It is necessary to retrieve the parameters when you want to set Out values, for example. The below code is also used when you want to retrieve the values of parameters of other jobs (replace the job object with an object that points to the correct job).
{
//get the In value of parameter Param1
JobParameter Param1Val = jcsJob.getJobParameterByName("Param1");
//print out the In value
jcsOut.println(Param1Val);
//or
jcsOut.println(parameters.Param1);
//set the Out value of the parameter
jcsJob.getJobParameterByName("Param1").setOutValue("GoodBye World!");
//print out the Out value
jcsOut.println(job.getJobParameterByName("Param1").getOutValue());
//write it to the database
jcsSession.persist();
}
Basic code to create a job from a job definition and submit the job, without specifying any parameters. If the job definition has parameters, the defaults will be used.
{
// code to submit a job running System_Info
// get the job definition
JobDefinition aJobDef = jcsSession.getJobDefinitionByName("RS_Test");
// create the job from the job definition
Job aJob = aJobDef.prepare();
// submit the job definition and write unsaved data to the database
jcsSession.persist();
}
The same basic code to submit a job used previously but this time with parameters. If you do not specify a value for a parameter, the default value will be used. Special care needs to be taken, if a job definition has a required parameter with no default value you must specify a value, or the job will not get submitted.
{
// code to submit SAP_AbapRun that runs the ABAP program RSUSR000
// get the job definition SAP_AbapRun
JobDefinition aJobDef = jcsSession.getJobDefinitionByName("SAP_AbapRun");
// create the job
Job aJob = aJobDef.prepare();
// assign parameters
aJob.getJobParameterByName("SAP_SYSTEMS").setInValueString("TR1");
aJob.getJobParameterByName("ABAP_PROGRAM_NAME").setInValueString("RSUSR000");
aJob.getJobParameterByName("JOBNAME").setInValueString("RSUSR000 created with RedwoodScript");
// assign queue as SAP jobs require a queue
Queue aQ = jcsSession.getQueueByName("TB7_Queue");
aJob.setQueue(aQ);
// submit the job definition and write unsaved data to the database
jcsSession.persist();
// wait for all children
jcsSession.waitForAllChildren(aJob);
}
A child job retrieves the parameter values of its direct parent.
{
//get the parent job
jcsJob parent = job.getParentJob();
//get the job parameters and respective values
JobParameter ParentParam1 = parent.getJobParameterByName("Param1");
JobParameter ParentParam2 = parent.getJobParameterByName("Param2");
JobParameter ParentParam1InValue = ParentParam1.getInValue();
JobParameter ParentParam2InValue = ParentParam2.getInValue();
//print out the values of the parent parameters
jcsOut.println("Parent Parameter Param is set to " + ParentParam1InValue);
jcsOut.println("Parent Parameter Param2 is set to " + ParentParam2InValue);
//set the Out value of the current (child) job
job.getJobParameterByName("childParam").setOutValue("Done");
jcsSession.persist();
}
A job chain job retrieves job chain-level parameters. Parameters are predefined as chainParameters.<parameterName> for the In value and outChainParameter.<parameterName> for the Out value. The following objects are also predefined:
stepIterations
returnCode
{
//get the job parameters and respective values
JobParameter chainParam1 = chainParameters.Param1;
JobParameter chainParam2 = chainParameters.Param2;
//print out the values of the parent parameters
jcsOut.println("Parent Parameter Param is set to " + chainParam1);
jcsOut.println("Parent Parameter Param2 is set to " + chainParam2);
//set the Out value of the current (child) job
job.getJobParameterByName("childParam").setOutValue("Done");
jcsSession.persist();
}
Alternatively, you can get the job chain job and then get its parameters, this example illustrates RedwoodScript without the use of the predefined parameters.
{
//get the step job (up one level)
jcsJob step = job.getParentJob();
//get the inner-most job chain job (up another level)
jcsJob parent = step.getParentJob();
//get the job chain parameters and respective values
JobParameter chainParam1 = parent.getJobParameterByName("Param1");
JobParameter chainParam2 = parent.getJobParameterByName("Param2");
JobParameter chainParam1InValue = ParentParam1.getInValue();
JobParameter chainParam2InValue = ParentParam2.getInValue();
//print out the values of the job chain parameters
jcsOut.println("Parent Parameter Param is set to " + chainParam1InValue);
jcsOut.println("Parent Parameter Param2 is set to " + chainParam2InValue);
//set the Out value of the current job
job.getJobParameterByName("JobParam1").setOutValue("Done");
jcsSession.persist();
}