Boundary Script API missing trim() function

In Connect 2021.2, writing a Boundary Script, I need to trim a scraped region/value. But I’m getting errors that “trim()” isn’t a function.I also tried “replace()” with a RegEx, which also fails.

Why doesn’t the Boundary API have access to Javascript string functions?

EDIT: This is a PDF job, with an account number on the top of most pages. If it isn’t there, that area will be blank and the page is part of the current record. So the task of the Boundary Script is to scrape the account number, and if it’s not blank/null, compare it to the account number saved into a boundary variable from the previous page/scraped region. If the number is not null, and doesn’t equal the boundary variable, time to set a boundary.

The problem is my two strings, even if equal, were evaluating as not equal. I suspected that I was picking up spaces or extraneous characters, so thought to trim() them both. That fails. I tried casting to INT via parseInt(), since the Account Number is numeric. The cast failed, giving me “NaN”, which confirmed that I was picking up space or junk characters.

I added vocaJS as an external library and used their v.trim() function, and that resolved the issue.

We really need access to string functions in Boundary Scripts!!

//read the Account Number into a JS variable zeAccount
var zeAccount = boundaries.get(region.createRegion(24.5,3.0,47.0,12.0));
var storedAcct = boundaries.getVariable("AcctNum");
 
//check to see if we've already processed a boundary and stored the account number into the "AcctNum" boundary variable
//if not, it will be null and we're on record one
if(storedAcct !=null) 
{ //we're here which means AcctNum exists and holds a value, so we are mid-record, possibly the start of a new record
  //we need to set a new document boundary IF the value we just scraped into "zeAccount" doesn't match the last value we scraped & stored,
  //AND the value we just scraped isn't null/blank
  if( (v.trim(zeAccount[0]) != v.trim(storedAcct)) && (zeAccount[0] != null) )
  {
    //set the new document boundary
    boundaries.set();
  }
}
 
//store the value we scraped into a boundary variable, unless it was null
//if it WAS null, we are on a blank page and don’t want to store a “false positive” null value as a document boundary
if(zeAccount[0] != null)
{
  boundaries.setVariable("AcctNum",zeAccount[0]);
}

It would help if you used JavaScript syntax instead of VBScript. :stuck_out_tongue:

To trim zeAccount[0], you would use zeAccount[0].trim() instead of v.trim(zeAccount[0]) (by the way, where do these v.trim(something) statements come from? There’s no declaration for a v variable anywhere!)

To replace something in zeAccount[0], you would use zeAccount[0].replace(/myRegEx/,"replacement")

I agree, using .trim() should work, but didn’t. Trying to .trim() any string results in:

Java class “[Ljava.lang.String;” has no public instance field or method named “trim”.

Same with replace, trying to do str.replace() throws the same error, that there is no such method.

The workaround was to find an external JS library. I found vocaJS. That’s where “v.trim()” came from, and that is what is working.

I tested it here, making sure I had some CSV fields that had spaces in front or after values, and I am able to group them according to the trimmed value, without any error:

var value = boundaries.get(region.createRegion("SomeValue"));
var myField = value[0].trim();
var lastValue = boundaries.getVariable("lastValue"); 

if (lastValue && (myField!=lastValue)) { boundaries.set() }

boundaries.setVariable("lastValue",myField);

What version of Connect are you using?

2021.2. I think the issue is casting… that zeAccount[0] isn’t being consistently typed as a string.

In this code, using the vocaJS library and it’s version of “trim” and “replace”, everything works fine.

//read the Account Number into a JS variable zeAccount
var zeAccount = boundaries.get(region.createRegion(24.5,3.0,47.0,12.0));
var storedAcct = boundaries.getVariable(“AcctNum”);
var scraped = v.replace(v.trim(zeAccount[0])," “,”");

//check to see if we’ve already processed a boundary and stored the account number into the “AcctNum” boundary variable
//if not, it will be null and we’re on record one
if(storedAcct !=null)
{ //we’re here which means AcctNum exists and holds a value, so we are mid-record, possibly the start of a new record
//we need to set a new document boundary IF the value we just scraped into “zeAccount” doesn’t match the last value we scraped & stored,
//AND the value we just scraped isn’t null/blank
if( scraped != storedAcct && scraped != “” )
{
//set the new document boundary
boundaries.set();
}
}

//store the value we scraped into a boundary variable, unless it was null
//if it WAS null, we are on a blank page and don’t want to store a “false positive” null value as a document boundary
if(scraped != “”)
{
boundaries.setVariable(“AcctNum”,scraped);
}

But when I replace the “var scraped…” line 3, with the version that should work with native JS functions:

var scraped = zeAccount[0].trim();

I get a bad record count and this error:

“Cannot call method trim of undefined”

Ok, mystery solved! “zeAccount[0]” is sometimes a string, sometimes it’s “null” because nothing was scraped from that region. And you can’t preform string functions on a null. With the following revised code, I can now use native JS .trim() and .replace() functions without needing an external library. Thank you, Phil, for the hints.

//read the Account Number into a JS variable zeAccount
var zeAccount = boundaries.get(region.createRegion(24.5,3.0,47.0,12.0));
var storedAcct = boundaries.getVariable("AcctNum");
var scraped;

if(zeAccount[0] == null)
{
  scraped = '';
}
else
{
  scraped = zeAccount[0].trim().replace(" ","");
}

 
//check to see if we've already processed a boundary and stored the account number into the "AcctNum" boundary variable
//if not, it will be null and we're on record one
if(storedAcct !=null) 
{ //we're here which means AcctNum exists and holds a value, so we are mid-record, possibly the start of a new record
  //we need to set a new document boundary IF the value we just scraped into "zeAccount" doesn't match the last value we scraped & stored,
  //AND the value we just scraped isn't null/blank
  if( scraped != storedAcct && scraped != "" ) 
  {
    //set the new document boundary
    boundaries.set();
  }
}
 
//store the value we scraped into a boundary variable, unless it was null
//if it WAS null, we are on a blank page and don’t want to store a “false positive” null value as a document boundary
if(scraped != "")
{
  boundaries.setVariable("AcctNum",scraped);
}