frangomintz
frangomintz

Reputation: 23

ServiceNow - Data Transform Script Debug

I am using a data transform to import a large .csv of emails into a Watchlist field. My .csv is in the following format:

"[email protected]", "[email protected], [email protected]",,"[email protected]"

I am using the following script to link the imported emails to User records, however my data transform ignores all records despite me manually confirming the emails exist, is there something wrong in this script?

answer = (function transformEntry(source) {

    var grUsers = new GlideRecord('sys_user');
    var users = String(source.u__users).split(",");
    var query = 'email=LIKE' + String(users[0]);
    var i = 1;
    while(i != users.length){
        query = query + '^ORemailLIKE' + String(users[i]);
        i++;
    }

    grUsers.addEncodedQuery(query);
    grUsers.query();
    return grUsers.join(',');

})(source);

Upvotes: 1

Views: 494

Answers (1)

Sergiu
Sergiu

Reputation: 4598

Two problems:

  1. Using split(",") is not enough if the format you are showing is correct. You need to strip the quotes as well
  2. Using email=LIKE should be emailLIKE, no = sign

Here is an example based on your data:

var data = '"[email protected]","[email protected]","[email protected]","[email protected]"';
var users = String(data).split(",");

for (var i = 0; i < users.length; i++){
 gs.print(users[i])
}

var query = 'email=LIKE' + String(users[0]);
gs.print(query);

Running this in Background Scripts I get:

*** Script: "[email protected]"
*** Script: "[email protected]"
*** Script: "[email protected]"
*** Script: "[email protected]"
*** Script: email=LIKE"[email protected]"

Here's the fixed one:

var data = '"[email protected]","[email protected]","[email protected]","[email protected]"';
var users = String(data).split(",");

for (var i = 0; i < users.length; i++){
 users[i] = users[i].replace(/^"(.*)"$/, '$1');
}

for (var i = 0; i < users.length; i++){
 gs.print(users[i])
}

var query = 'emailLIKE' + String(users[0]);
gs.print(query);

Result:

*** Script: [email protected]
*** Script: [email protected]
*** Script: [email protected]
*** Script: [email protected]
*** Script: [email protected]

Upvotes: 1

Related Questions