Arrays and List Fields

kemmy1
Tera Guru

I having trouble with an array.  I'm trying to find if any of the choices that were selected is a Tier 3 group type.  If so, then stop processing and make tier3 = true.  So I hard coded the array and tested it on the BackGround Script:

var tor = "BigPipe,Cell Base,Cloud Connectivity";
    var tor_array = tor.split(',');
    var i;
    var grouptype;
    var tier3;
    var endLoop;
    
    var gr = new GlideRecord('u_onenet_type_of_request');
    gr.addQuery('u_active', true);
    gr.query();
    while(gr.next()){
        for (i=0; i < tor_array.length; i++) {
            gs.log("LAK length " + tor_array.length);
            if (gr.u_type_of_request == tor_array[i]){
                grouptype = gr.u_onenet_group_type;
                gs.log("LAK Type of Request " + gr.u_type_of_request);
                gs.log("LAK Grouptype " + grouptype);
                if(grouptype == 'Tier 3'){
                    tier3 = true;
                } else {
                    tier3 = false;
                }
            }
            if (tier3 == true){
                endLoop = true;
                break;
            }
        }
        if (endLoop == true){           
            break;
        }
    }

This works like a charm, but when I change the tor variable to: "BigPipe, Cell Base, Cloud Connectivity"; (with spaces) I only get info back from the BigPipe variable.

Problem is when I actually script out var tor_array = current.variables.ref_type_of_request.getDisplayValue().split(','); and I look to see what's in there, it's "BigPipe, Cell Base, Cloud Connectivity";

Are the spaces making a difference? Or is it something else?

 

Lisa

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kemmy,

yes spaces would make a difference.

A string with "hello" and "hello " has a difference and the query won't run properly.

better trim the leading and trailing spaces using trim() method

            if (gr.u_type_of_request == tor_array[i].trim()){

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kemmy,

yes spaces would make a difference.

A string with "hello" and "hello " has a difference and the query won't run properly.

better trim the leading and trailing spaces using trim() method

            if (gr.u_type_of_request == tor_array[i].trim()){

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

THANK YOU!!!  This worked like a charm!

Hi Kemmy,

Can you also mark my answer as helpful? Thanks in advance.

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader