This section gives an example of the following kinds of scripts:
Assignment Script
The following assignment script assumes that the process instance has data fields reviewer1 and reviewer2 that contain the common name for two reviewers. If one or both of the reviewer1 and reviewer2 fields contain common names for valid users, the work item is assigned to the valid named reviewers. Otherwise it is assigned to the process instance creator.
The default value for the reviewer1 and reviewer2 fields is an empty string. The script checks that the reviewers exist as users in the corporate directory, and if so, gets their distinguished names (DNs) and puts them in an array. Then just for good measure, the script double-checks that the array contains valid user DNs only, and if it does, returns the array. If the array does not contain valid DNs, the script assigns the work item to the creator of the process instance.
function assignToNamedReviewers ()
{
// Get the process instance.
var pi = getProcessInstance ();
var corp = getCorporateDirectory ();
// Create an array to contain the assignees.
var assigneeArray = new Array ();
// Get the common names of reviewer1 and reviewer2.
// The default value of these fields is an empty string.
var reviewer1 = pi.getData ("reviewer1");
var reviewer2 = pi.getData ("reviewer2");
// If reviewer1 has a name, find them in the corporate directory.
if (reviewer1 != "") {
var reviewer1CN = corp.getUserByCN (reviewer1);
// If we have a user for reviewer1, get their DN
// and add it to the assignee array.
if (reviewer1CN != null) {
var reviewer1DN = reviewer1CN.dn;
assigneeArray.push (reviewer1DN);
}
}
// Do the same thing for reviewer2.
if (reviewer2 != "") {
var reviewer2CN = corp.getUserByCN (reviewer2);
if (reviewer2CN != null) {
var reviewer2DN = reviewer2CN.dn;
assigneeArray.push (reviewer2DN);
}
}
// If the assignee array is not empty and each element is a valid dn
// return the assignee array otherwise
// return an array of the dn of creator of the process instance.
if ((assigneeArray.length != 0) && (checkUserDNs (assigneeArray))) {
return assigneeArray;
}
else {
return new Array (pi.getCreatorDN());
}
}
Expiration Setter Script
This expiration setter script gets the value of the pageCount field and uses it to determine whether reviewers need a long or short review period. If the page count is greater than 100, the review period is long (14 days), otherwise the page count is short (7 days).
function setEndOfReviewPeriod(){
// Get the process instance.
var pi = getProcessInstance();
// Get the page count.
var pageCount = pi.getData ("pageCount");
// The review period is based on the page count.
// For pages < 100, expire in 7 days.
// For pages > 100, expire in 14 days.
var reviewPeriod;
if (pageCount > 100) {
reviewPeriod = 14;
}
else {
reviewPeriod = 7;}
var now = new Date();
var nowTime = now.getTime(); //UNIX epoch time
var expTime = nowTime + (reviewPeriod * 24 * 60 * 60 * 1000);
return new Date(expTime);
}
Expiration Handler Script
This expiration handler script reassigns the work item to the creator. In this particular case, the task of the work item is to review a document, whose name is stored in the docname data field.
function reviewPeriodExpired(){
// Get the process instance and the work item.
var pi = getProcessInstance ();
var wi = getWorkItem();
// Re-assign the work item to the creator
var creatorArray = new Array (pi.getCreatorDN());
wi.assignTo(creatorArray);
// Get the document name.
var docname = pi.getData("docname");
// getCurrentActivityCN() is a work item method that returns
// the name of the work item.
var activityName = wi.getCurrentActivityCN();
// Return true to indicate successful expiration.
return true;
}
Completion Script
The following completion script checks that the value of the pageCount data field is a number between 1 and 3000 inclusive. If the pageCount is too high, too low, or is not a number, the completion script adds an informative error message to the error dialog box and returns false.
function checkPageCount(){
var pi = getProcessInstance ();
var pageCount = pi.getData("pageCount");
// The standard JavaScript function parseInt gets an integer
// from a string if it has one, otherwise returns 0.
// For example, parseInt("33hello") returns 33.
// If the first character cannot be converted to a number,
// parseInt returns "NaN".
var pageCountNum = parseInt(pageCount);
// The standard JavaScript function isNaN
// tests if a value is not a number.
if ((isNaN (pageCountNum)) ||
(pageCountNum < 1) || (pageCountNum > 3000)) {
setErrorMessage ("You entered " + pageCount +
" as the page count. This value is invalid. " +
"Please enter the page count as a number " +
"between 1 and 3000 inclusive.");
return false;
}
else return true;
}
Automation Script
This script updates a link to a newly submitted document in a file that lists all documents available for review.
This script could be used as an automation script in a process that enables writers to submit documents for review. In the entry point form, the writer uses a File attachment data field called docFileName to attach the document. Many different writers can use the application to submit documents. The file docList.html contains an active link for every document that has been submitted with this application. The file docList.html lives in the top level of the content store so that all process instances in the application can access it.
Two files are involved in the document list. The file docList.html is an HTML-formatted list of the documents with an active link for each document. The file showList.html contains opening HTML tags, the list of documents (which is the same as docList.html), and closing HTML tags. This script uses two files so that it can add the closing HTML tags after adding the information for the newly submitted document.
function updateDocReviewWebPage(){
// Get the process instance.
var pi = getProcessInstance();
// docList.html and showList.html are stored at the
// top level in the content store.
// Get the root URL for the content store.
var myStore = getContentStore();
var storePath = myStore.getRootURL();
// Get the full pathnames to docList.html and showList.html.
var docListPath = storePath + "docList.html";
var showListPath = storePath + "showList.html";
// Get the contents of the doc list.
var docList = myStore.getContent(docListPath);
// Get the pathname to the doc that was submitted for review.
var docURL = pi.getData("docFileName");
// Get info about the newly submitted document.
var docname = pi.getData("docname");
var writer = pi.getData("writername");
var writerComments = pi.getData("writerComment");
// Add a link to the newly submitted doc in the doc list.
docList += "<A HREF=" + docURL + ">";
docList += "<H2><FONT COLOR='#55CCAA'>" + docname +" </H2></FONT></A>";
docList += "<P>Author: " + writer + "</P>";
docList += "<P>Author comments: </P>";
docList += "<BLOCKQUOTE>" + writerComments + "</BLOCKQUOTE><HR>";
// Store the doclist back into docList.html
myStore.store (docList, docListPath);
// Create the final content, which contains the heading tags,
// the doc list, and the closing tags.
var finalContent = "<HTML><HEAD><TITLE>Documents available for review" +
"</TITLE></HEAD>";
finalContent += "<BODY><CENTER><H1>Documents Available for Review" +
"</H1></CENTER>" +
"<P>We encourage feedback on these documents from everyone.</P>" +
docList + "</BODY></HTML>";
myStore.store (finalContent, showListPath);
return true;
}
Figure 15.6 illustrates the showList.html web page. This page is updated each time a document is submitted for review.
Figure 15.6    A web page that is updated automatically