Skip to main content

JavaScript Cookbook

Below you wil find some examples of processors created to modify content and data with the JavaScript Processor.


Remediate Illegal Characters

Remove illegal characters on fields.

var id = rd.getSimflofySourceRepositoryId();

//Remove the version from the document ID:
rd.addSingleField('shortId', id.substring(0, id.indexOf(':')));

//Grab fields we want to clean.
var name = rd.getFileName();
var title = rd.getDocumentField('Document.title');
var description = rd.getDocumentField('Document.description');

name = cleanText(name, true);
//Set the cleansed name.
rd.setFileName(name);

//Check title
if (title != null) {
var titleVal = title.getFieldValue();
titleVal = cleanText(titleVal, false);
rd.addSingleField('Document.title', titleVal);
}

//Check desciption
if (description != null) {
var descVal = description.getFieldValue();
descVal = cleanText(descVal, false);
rd.addSingleField('Document.description', descVal);
}

//Function to clean illegal characters.
function cleanText(textToClean, isName) {
textToClean = textToClean.replace(/[^\S ]/g, ' ');
textToClean = textToClean.replace(/[โ€”โ€“โ€]/g, '-');
textToClean = textToClean.replace(/[โ€œโ€]/g, "'");
textToClean = textToClean.replace(/[^a-z0-9\- ()_\/\.\s'"{}&+]/gi, "_");

//Additional clean up for document names. (Includes Encoding)
if (isName) {
textToClean = textToClean.replace('{', '%7B');
textToClean = textToClean.replace('}', '%7D');
textToClean = textToClean.replace(/['"]/g, '%27%27');
}
return textToClean;
}

Set a Date Field Using Java Date

Use a Java Date field to set the date of a property if it does not exist.


//Create a variable with the Java type.
var jDate = Java.type("java.util.Date");

var eventDate = rd.getDocumentField("Document.Event Date").getFieldValue();

//If the date does not exist on the document, use the current date.
if (!eventDate || eventDate == "") {
eventDate = Date.now();
}

rd.addSingleDateField("eventDate", new jDate(eventDate));

Using a Switch Statement


var region = rd.getDocumentField("Document.Region").getFieldValue();

switch (region) {
case "SE":
region = "US - Southeast Region";
break;
case "NE":
region = "US - Northeast Region";
break;
case "MW":
region = "US - Midwest Region";
break;
case "RM":
region = "US - Rocky Mountain Region";
break;
case "SW":
region = "US - Southwest Region";
break;
case "P":
region = "US - Pacific Region";
break;
default:
region = "-Select-";
}

rd.addSingleField("region", region);

Remove Fields With No Value


var map = rd.getFieldsMap();
for (i in map) {//Loop through all the keys in the map
var val = map[i].getFieldValue();//Grab the value of the DocumentField
if (val === '') {//Check if the value is empty
log.info('Removing ' + i);
rd.removeField(i);
} else {
//do something else, or omit this else statement
}
}

Convert ACLs

This example converts box editors to google drive writers.

if(rd.getACL() != null){
var newAcl = [];
for (var i in rd.getACL()){
var acl = rd.getACL()[i];
var split = acl.split('=');
var nRole = ''
if(split[1] === 'editor'){
nRole = 'writer'
}else{
nRole = split[1];
}
newAcl[i] = split[0] + '=' + nRole;
}
rd.setACL(newAcl);
}

Remove Folders From a Parent Path


var skipFolders = 1; //This is how many folders to remove from the path
if(skipFolders > 0){
var absPath = rd.getPath();//Retrieving the parent path of the document
if(absPath.indexOf('/') === 0){//Removing a leading slash, as they causes issues with the split
absPath = absPath.substring(1);
}
var splitPath = absPath.split('/');//Split the path into parts
var parts = splitPath.length;//Check how many parts the path is

var nupath = [];//Create an array to contain the new path

for(var i = skipFolders ; i < parts ; i++){//Start loop after the skipped parts
nupath.push((splitPath[i]));//Add the part to the array
}
rd.setPath("/" + nupath.join('/'));//Combined the new path into a parent path with a leading slash
}