Getting a subset of string from multivalue field SSJS

286 Views Asked by At

In an XPage bound to a document I have a multivalue field containing email addresses. I simply wish to loop through the email addresses and return a subset of addresses which contain mydomain. To then use them in 'recipient' field for emailing.

Seems straight forward, but I receive the following error, even though 'typeof()' returns strings. I can't seem to get away from the [JavaScript Object]. Tried toString() and all sorts. Any help is appreciated.

Error message Error while executing JavaScript computed expression Script interpreter error[TypeError] Error calling method 'includes(string)' on an object of type 'String [JavaScript Object]'

JavaScript code

  var array = documentContract.getItemValueArray("EmailAddress")
  var result = "";
  for (var i=0; i<array.length; i++) {
  if ( array[i].includes("@mydomain.com"))  { 
  result = result + array[i] + "\n";}
  }
  return result;
2

There are 2 best solutions below

2
Per Henrik Lausten On BEST ANSWER

The error message says that includes is not an available method on the string object.

You can use @Contains:

if (@Contains(array[i], "@mydomain.com"))  { 
0
D.Bugger On

Gotta love Formula language:

evaluate('@Trim(@Left(EmailAddress, "@mydomain.com"))+"@mydomain.com"', documentContract)

Watch out for uppercase and no mydomain at all. Maybe the evaluate isn't even necessary, it might just function in SSJS without it.