Script Launcher (XF000)

The type for functions to execute JavaScript. These scripts are called "Function Scripts" in comparison to "Table Scripts". Various processes are defined by this type like JCL processes, Dialog processes, Database processes, and Web Services.

Driver sample of Script Launcher with a dialog(shown foreground)


Editor definition of the function above


Elements to be defined
< Basic Attributes>
Function Type - Fixed value.
Function ID - Fixed and unique value in the system.
Function Name - Editable value to express function role.
Console - Option to show console. With "Show" selected, job will show console to indicate process status. With "Timer" selected, job will show console with timer configurations. If you select option to show console, avoid to use method of session dialog in scripts because of unpredicable failures.
Default Time - Default timer configuration. Specify the time to start prosess like 20:00. Or specify list of times like LIST(20:00, 21:00, 22:00). You can even specify range of time and interval seconds like REPEAT(09:00, 18:00, 300). NOW is the value to show dialog in instant-start mode.
Message - The message of the dialog shown when timer job starts.

< Script>
Codes written in JavaScript. You can put session object and instance object in script. You can use keys of Ctrl+I(Indent), Ctrl+N(Name mode), Ctrl+/(Comment), Ctrl+Z/Y(Undo/Redo), F3(Scan string), F7(Check syntax error).

You can use Java classes in script. Following is an example.

// Show Confirm Dialog of Swing JOptionPane //
var answer = javax.swing.JOptionPane.showConfirmDialog(null, '...', '...', javax.swing.JOptionPane.OK_CANCEL_OPTION);
if (answer == javax.swing.JOptionPane.OK_OPTION) {
...
}

This is an alternative coding using JavaImporter and with syntacs. Note that you had better avoid using session object in with syntacs for an unknown reason.

// Importing Java Swing Package //
var importedPackage = new JavaImporter(javax.swing, java.util);
with (importedPackage) {

 // Show Confirm Dialog of Swing JOptionPane //
 JOptionPane.showConfirmDialog(null, ...);
 ...
}

You can control font and colors of the editor pane by editting a file named "xteaedt.properties" on the Driver folder. Here is an example to set background as black and foreground as green and caret as white. Note that you had better specify monospaced font for ScriptFont.

# X-TEA Editor Properties
MainFont = Meiryo UI
ScriptFont = MeiryoKe_Gothic
ScriptFontSize = 20
ScriptBackground = 0,0,0
ScriptForeground = 0,255,128
ScriptCaret = 255,255,255

< Where-Used>
The list of functions using this function. Right-click on the row to jump to the definition.


Coding sample with Option Dialog
This is a coding sample of a process with OptionDialog of session object. It will call a function according to the option chosen.

var dialog = session.getOptionDialog();
dialog.addOption('Call AF100'); // 0 //
dialog.addOption('Call BF100'); // 1 //
dialog.addOption('Cancel'); // 2 //
var reply = dialog.request('Choose Function to be Called');
if (reply == 0) {
   instance.parmMap.put('FIELDA', '10');
   instance.callFunction('AF100');
}
if (reply == 1) {
   instance.parmMap.put('FIELDB', '20');
   instance.callFunction('BF100');
}
if (reply == 2 || reply == -1) {
   instance.returnMap.put('RETURN_MESSAGE', 'Calling Canceled.');
}


Coding sample with database operations
This is a coding sample of a process with database operations. It receives a parameter value of NRORDER and select/update a specific record of ORDER table. Refer to instance object for details of TableOperator.

// Variants //
var rORDER; var uORDER; var rVENDOR;
var count = 0; var errorMessage = '';

// Select ORDER //
rORDER = instance.createTableOperator('Select', 'ORDER');
rORDER.setSelectFields('NRVENDOR');
rORDER.addKeyValue('NRORDER', instance.parmMap.get('NRORDER'));
if (rORDER.next()) {

   // Select VENDOR //
   rVENDOR = instance.createTableOperator('Select', 'VENDOR');
   rVENDOR.setSelectFields('CDCOUNTRY');
   rVENDOR.addKeyValue('NRVENDOR', rORDER.getValueOf('NRVENDOR'));
   if (rVENDOR.next()) {

      // Call function according to vendor country //
      if (rVENDOR.getValueOf('CDCOUNTRY') == 'JPN') {
         instance.callFunction('FF016A'); //in Japanese//
      } else {
         instance.callFunction('FF016B'); //in English//
      }

      // Update DTISSUE of ORDER //
      uORDER = instance.createTableOperator('Update', 'ORDER');
      uORDER.addValue('DTISSUE', session.getToday());
      uORDER.addKeyValue('NRORDER', instance.parmMap.get('NRORDER'));
      count = uORDER.execute();
      if (count == 1) {
         instance.returnMap.put('RETURN_CODE', '10');
         instance.returnMap.put('RETURN_MESSAGE', '* P/O issued successfully.');
      } else {
         errorMessage = 'It failes to update ORDER.';
      }
   }
}

// Error Handling //
if (errorMessage == '') {
   instance.commit();
} else {
   instance.cancelWithMessage(errorMessage);
}

Be aware of database transactions and commiting in updating tables. With all updatings done successfully, you need to commit expressly at the end of functions as shown above. Even if you missed comitting, pending transactions will be commited impliedly at the end of sessions. But this will result in that other jobs will be left read-waiting to avoid dirty reads*2. The point is that you don't need to commit transaction in function if it is called by the other function which is made to commit. As a matter of fact, the sample function shown above has no need to commit as it receives a parameter which means it is called by the other function!

Now it's about rollback. Although you can put rollback explicitly in script, rollbacking is impliedly done if function terminates with RETURN_CODE as '99' which means it has aborted. And as shown above if instance.cancelWithMessage(errorMessage) is executed, it will rollback impliedly.

*2.Dirty reads is operations to be avoided. It means to read a record which is not commited after updated by the other job. Pending transactions result in nonrepeatable reads, phantom reads as well as dirty reads. As transaction isolation of X-TEA Driver is set as "TRANSACTION_READ_COMMITTED"(Java default), dirty reads is avoided but nonrepeatable reads and phantom reads are inavoidable.


Coding sample of Web Service
Following is a sample code of a Web Service. You can not put steps of UI nor print in the functions as they run in batch process. This is a sample function that receive XML formated data to be processed from a customer and return XML/JSON data as a result. Refer to session object for methods to process XML/JSON data.

// Validate customer code and password in request //
var cdCustomer = instance.parmMap.get('COMPANY')
var password = instance.parmMap.get('PASSWORD')
if (isValidCoustomer(cdCustomer, password)) {

   // Process order in request //
   var order = instance.parmMap.get('ORDER');
   var document = session.parseStringToGetXmlDocument(order);
   var orderHead = session.getXmlNode(document, 'head');
   var orderItemList = session.getXmlNodeList(document, 'item');
   for (i= 0; i < orderItemList.size(); i++) {
      …
   }

   // return XML document as a result //
   var result = session.createXmlDocument('result');
   …
   instance.returnMap.put('RESULT', result)

} else {
   instance.returnMap.put('RESULT', 'Customer code or password is invalid.')
}

// Subroutine to validate customer code and password //
function isValidCustomer(cdCustomer, password) {
   …
   return true
}


Other coding tips
Refer to Table Script for various tips.


To Function Type List