Task-Oriented Script Module

You can define what a custom step does using a task-oriented CommonJS script module that exposes a function to be called as the main function for the job step. When administrators create jobs using Business Manager, they set parameters that are available as scriptable objects for the module's function and for the dw.job.JobStepExecution object. The dw.job.JobStepExecution object allows read-only access to information about the current step execution and job execution. 

To control the exit status, the script module's function can return a dw.system.Status object. If the script finishes with an unhandled exception, the exit status code is ERROR, and the error status flag is true by default. If no status object is returned and no exception occurs, the status code is OK by default. 

The following example is a task-oriented CommonJS module.

var net = require( 'dw/net' );
var system = require( 'dw/system' );
var job = require( 'dw/job' ); 
 
const ConnectionFailedError = "ConnectionFailedError";
const FileNotFoundError = "FileNotFoundError";
 
function downloadFromFTP( hostURL, timeout, fileName )
{
  var ftpClient = new net.FTPClient();
  ftpClient.setTimeout( timeout );
  ftpClient.connect( hostURL );
  if( !ftpClient.connected )
  {
    throw ConnectionFailedError;
  }
  else
  {  
    ...
    if(file==null)
    {
      throw FileNotFoundError;
    }
    ...
    ftpClient.getBinary(...)
  }
}
 
function downloadFromSFTP(...)
{
  ...
}

exports.ftpDownload = function( parameters, stepExecution )
{
  try
  {
    if( parameters.ftpType == 'sftp' )
    {
      downloadFromSFTP( parameters.hostURL, parameters.timeout, parameters.fileName, parameters.userID, parameters.password );
    }
    else
    {
      downloadFromFTP( parameters.hostURL, parameters.timeout, parameters.fileName );
    }
 
    return new system.Status(system.Status.OK);
  }
  catch(ConnectionFailedError)
  {
    return new system.Status(system.Status.ERROR,null,"Connection Failed!");
  }
  catch(FileNotFoundError)
  {
    return new system.Status(system.Status.OK,"FILE_NOT_FOUND","The file could not be found.");
  }
}