Flow Designer GlideRecord vs Client Script

Corey Allgood
Kilo Contributor

Hello,

I'm working on a Flow Designer action that is meant to pull a list of records from a GlideRecord and output the response to an array. It seems to be working with a caveat.

    var company = inputs.company;
    var contacts = [];
    var grContacts = new GlideRecord(${CONTACTS_TABLE});
    grContacts.addQuery(${COMPANY_FIELD}, company);
    grContacts.query();
 
    while (grContacts.next()) {
      contacts.push(grContacts.${EMAIL});
    }
 
The expected response (and the behavior I see running this identical code from a client script) would be:

["Me@no-reply.com","User1@no-reply.com","User2@no-reply.com"]

What I'm seeing in Flow Designer is actually that it iterates the number of times, but duplicates the value that number of times, so it would be:

["User2@no-reply.com","User2@no-reply.com","User2@no-reply.com"]

 

Could someone please tell me where I'm going wrong?

 

Thanks.

1 ACCEPTED SOLUTION

Lookup Records followed by a ForEach will iterate over the records and you can build the array into a Flow Variable.  

My code did exactly what was suggested regarding the pass by reference issue, I used the toString() method to force it to be a string.

Another issue can be that you are not getting what you think with the grContact.${EMAIL}.  How about like this:

var company = inputs.company;
var contacts = [];
var email = '';
var contacts = new GlideQuery(${CONTACTS_TABLE})
    .where(${COMPANY_FIELD}, company)
    .select(${EMAIL})
    .map(function (contact) { return contact.${EMAIL}; })
    .toArray();

GlideQuery is worth a look, it can do some pretty nifty things.

Please mark as correct/helpful if this helps.

Thanks,

Aoife

View solution in original post

6 REPLIES 6

Aoife
Tera Guru

First, wondering why you are scripting this instead of just using Lookup Records from the CORE actions and then do a ForEach to process them, can easily generate an array of emails if that is the desire.

However, this is a known issue with tight looping in JavaScript within ServiceNow.  Try this:

var company = inputs.company;
var contacts = [];
var email = '';
var grContacts = new GlideRecord(${CONTACTS_TABLE});
grContacts.addQuery(${COMPANY_FIELD}, company);
grContacts.query();
 
while (grContacts.next()) {
   email = grContacts.${EMAIL}.toString();
   contacts.push(email);
}

Please mark as correct/helpful if this helps.

Thanks,

Aoife

Hi,

 

The reason I'm not using the look up records CORE action is that I'd need to iterate through each record to pull one field out. What I'm trying to accomplish is to pull a list of emails out to add to a glide list on my application form.

 

I did try your code, but got the same result. Another poster posted an article about pass by reference, so I think I'll bury my nose in that for a bit. Thanks for replying!

Lookup Records followed by a ForEach will iterate over the records and you can build the array into a Flow Variable.  

My code did exactly what was suggested regarding the pass by reference issue, I used the toString() method to force it to be a string.

Another issue can be that you are not getting what you think with the grContact.${EMAIL}.  How about like this:

var company = inputs.company;
var contacts = [];
var email = '';
var contacts = new GlideQuery(${CONTACTS_TABLE})
    .where(${COMPANY_FIELD}, company)
    .select(${EMAIL})
    .map(function (contact) { return contact.${EMAIL}; })
    .toArray();

GlideQuery is worth a look, it can do some pretty nifty things.

Please mark as correct/helpful if this helps.

Thanks,

Aoife

Thanks, I rethought my look up records CORE action, and it turns out I was thinking too much and accomplished what I needed by doing a

for (record),

  set flow variable ${emails} = (${flow_variable->emails}),(${contact_record->email})

 

Thanks again!