The script dictionary contains all the scripts that exist by default in any Process Builder application, and also the additional ones included in the data sheet application.
The data sheet application contains the following scripts in addition to the standard ones:
These scripts demonstrate how to create scripts of some of the types you will need to create in your own application. They also show how to take advantage of the global scripts, for example, getProcessInstance(), which returns a ProcessInstance object which can be used to retrieve information about the process instance.
The buildDS Script
The automated activity that builds the database uses buildDS. If you double-click buildDS in the application tree view, you see the text of the script.
// Building the datasheet
// 1. LookUp discount information
// 2. Create and publish datasheet
function buildDS()
{
return lookupCode()&&buildDataSheet();
}
This script calls other scripts in the toolkit. You add scripts to the toolkit if you want to be able to reuse them within the application.
This script executes each of the following scripts:
The use of the && operator means that the functions are evaluated in order left to right, and so long as each function returns true, the next function is evaluated. If all the functions return true, the statement returns true. If any of the functions return false, the remaining functions will not be evaluated, and the function returns false.
The lookupCode Script
The lookupCode script computes the value of the price_final field using the price and discount code that the user entered.
function lookupCode (){
// The global function getProcessInstance()
// returns the process instance.
var pi = getProcessInstance();
// The methods getData and setData of the process instance are used
// to read the data field and set the value of the datafield. When
// setting a value, you have to provide the right JavaScript type.
// Retrieve the discount_code (which is set in the creation form).
var discount_code= pi.getData("discount_code");
// Note that discount_code is an element of the data dictionary.
// Construct the discount description using the discount_code.
var desc = "Preferred VAR discount (code " + discount_code + ")" ;
// The discount_codeis something like '10 A'.
// Extract the numerical discount from the discount_code.
// Note that parseInt('10 A') returns 10.
var disc = parseInt(discount_code);
// If the discount is not a number (isNaN)
// add an entry in the error log and return false.
if (isNaN(disc)){
var e = new Object();
e.inFunction = "lookUpCode";
e.discountCode = discount_code;
e.computedDiscount = disc;
logErrorMsg("ERROR_DISCOUNT_IS_NOT_VALID",e);
// Return false to ask the engine to rollback the transaction.
return false;
}
// Update the process instance data by adding the
// discount description, discount rate, and final price.
pi.setData("discount_desc", desc);
pi.setData("discount",disc.toString() );
pi.setData("price_final", Math.round(
parseFloat(pi.getData("price"))*(100-disc))/100);
// Return true because the script has been successfully performed.
return true ;
}
The buildDataSheet Script
This script creates a data sheet as an HTML file. It gets the data from the type, model, price, and discount code values that the user entered in the newDataSheet.html form at the entry point to this process. Additionally, the script opens a file that contains a description of the item in question, and adds the description to the data sheet. The script uses an HTML template file as the template for the datasheet, and ultimately publishes the datasheet as an HTML file.
The HTML template contains place holders identified by $$ to indicate places where values of data fields should be plugged into the template. For example, the place holder $$model$$ will be replaced by the value of the model data field.
For a sample of the description.txt file, see "The description.txt File" on page 235. For a sample of the printer.html template file, see "The printer.html Template File" on page 237
function buildDataSheet (){
// Use the global function getProcessInstance to get a reference to
// the process instance.
var pi = getProcessInstance();
// Get the value of the description data field, which is a URL
// pointing to where the description text file is located.
var descriptionURL = pi.getData("description");
// Use the global function getContentStore to get a reference
// to the content store.
var cStore = getContentStore();
// Read the content of the description file.
var descriptionContent = cStore.getContent(descriptionURL);
// Get the path name of the application.
var appPath = getApplicationPath();
// Create a connection to the file containing the template to be
// used to format the datasheet. The filename is something like
// appPath/models/printer.html
var template =new File (appPath + "models/"+ pi.getData("type")
+ ".html");
// Get the model.
var model = pi.getData("model");
// Construct the URL where the datasheet will be published.
// The global function getBaseForFileName returns the base URL of the
// location in the content store reserved for the process instance.
var dsURL = getBaseForFileName(pi.getInstanceId()) + model
+ ".html";
// Create the string to hold the content for the datasheet.
var dsContent = "<!-- Content of the dataSheet -->";
// Open the template in read-only mode. If the file is not opened
// successfully, log an error and return false.
if (!template.open("r")){
template.close();
var e = new Object();
e.model = model;
e.template = template;
e.stagingURL= dsURL;
logErrorMsg("ERROR_COULD_NOT_OPEN_TEMPLATE_FOR_READ",e);
return false ;
}
// Merge the template with data field values and with the content in
// the description.txt file. Values of data fields are plugged into
// corresponding placeholders in a template. For example, $$model$$
// in the template is replaced by the value of the model data field.
// Paragraphs in the description.txt file are substituted into
// the template in place of $$n$$ placeholders. For example, $$2$$ in
// the template is replaced by the second paragraph in
// description.txt.
var partArray = descriptionContent.split("----");
while (!template.eof()){
// Read a line from the template file and split it into strings
// using $$ as the separator. Create an array of the strings.
// param[0] is the first string, param[1] is the second string, etc.
// For example:
// <TD WIDTH="30%"> <FONT SIZE=+1>$ $$price_final$$</FONT></TD>
// param[0] = <TD WIDTH="30%"> <FONT SIZE=+1>$
// param[1]= price_final
// param[2] = </FONT></TD>
var line = template.readln();
var param = line.split("$$");
// If there are 3 sections, substitute either a description
// paragraph or a datafield value for the middle string.
if (param.length == 3){
var ind = parseInt(param[1]); // Returns 0 if not an integer.
// If ind is > 0 get a paragraph from description.txt
// else get a data field value.
var subthis = ind > 0 ? partArray[ind-1] pi.getData(param[1]);
// Write the first string into the datasheet.
dsContent += param[0];
// Write the substituted string into the datasheet.
dsContent += subthis;
// Write the last string into the datasheet.
dsContent += param[2];
}
// If there are not 3 strings, it means the line contains no
// placeholders, so write the whole line into the datasheet.
else {
dsContent += line;
}
dsContent+="\n";
}
// Close the template.
template.close();
// Use the store method of the contentStore object to publish the
// datasheet to the destination URL that was defined earlier.
var status = cStore.store(dsContent,dsURL);
// Keep track of where the datasheet has been published by storing
// the URL in a data field.
pi.setData("stageURL",dsURL);
return true;
}
The computeTitle Script
This script is invoked every time a data sheet is initiated. It is responsible for initializing the title of the process instance. The title of the process instance shows up in the work list when the application is running in production mode.
This script relies on the fact that the application contains a field called title that is used as the title field for the application (that is, in the application's properties, the title property is set to title.)
Users enter the values for the type and model during the entry point. This script runs as a completion script for the entry point. The script simply gets the value of the type and model data fields, and combines them to specify the name of the process instance. For example, if the type is printer and the model is 2345, the title of the process instance is "printer 2345".
function computeTitle(){
// Use the global function getProcessInstance() to get a reference to
// the process instance.
var pi = getProcessInstance();
// Read the value of the field "model" which was set by the
// participant in the newDataSheet form in the entry point.
var model = pi.getData("model");
// Read the value of the field "type" which was set by the
// participant in the newDataSheet form in the entry point.
var type = pi.getData("type");
// Set the value of the field "title" to be an aggregation
// of the type and the model.
pi.setData ("title", type + " " + model);
// The execution has been successful.
return true;
}