How to get values in List field?

ktjstn
Kilo Expert

I have a List field named Servers. I just want to know if one of the selected servers has a Production environment and will return True. If there is no Production environment on one of the selected servers in the field I want it to return False.

But I'm getting this result:

find_real_file.png

Here's my script:

var servers = '06cb8165dbebdf444f9596a9db96190d,35efb9b6db231b00906d791c8c96198d,3defb5b6db231b00906d791c8c9619f0,e4bffdbadbeb9740b9b19c16db961934,6ad627dadb7d3704b9b19c16db961985,edb4f806db35bbc040067b668c96190e,4ffb97d6db393704b9b19c16db9619e1';

var serverArray = servers.split(',');

	for (var i = 0; i < serverArray.length; i++) {
		var gr = new GlideRecord('cmdb_ci_server');
                    

		if (gr.get(serverArray[i])) {

			if(gr.u_environment == 'Production')
                        {
                            gs.print('true');
                        }else{
                           gs.print('false');
                        }  
 

  
		}
                
                
	}
1 ACCEPTED SOLUTION

Andrew Saxton -
Tera Expert

In your code as you loop through the servers, you are telling the system to print true/false each time. Its going to do just that. I'm assuming you want it to return back if any of them are true, then this should work:

var servers = '06cb8165dbebdf444f9596a9db96190d,35efb9b6db231b00906d791c8c96198d,3defb5b6db231b00906d791c8c9619f0,e4bffdbadbeb9740b9b19c16db961934,6ad627dadb7d3704b9b19c16db961985,edb4f806db35bbc040067b668c96190e,4ffb97d6db393704b9b19c16db9619e1';
var prodServer = false;
var serverArray = servers.split(',');
    //Looping through each server in the array...
	for (var i = 0; i < serverArray.length; i++) {
		var gr = new GlideRecord('cmdb_ci_server');
                    

		if (gr.get(serverArray[i])) {

			if(gr.u_environment == 'Production'){
                gs.log('true')
                prodServer = true;
                //Telling the system to stop looping since we found a production server
                break
            }else{
                //Continue through the loop instead.
                gs.log('false')
            }
 

  
		}
                
                
	}

 

prodServer will only be true if it found a prod server, otherwise it will stay false.

 

Alternatively, you could replace the break with a return if you plan to put this code in a function, and just return true. That way if you loop through all of them without returning, you could return false since you can assume that there wasn't a production server in the list.

View solution in original post

2 REPLIES 2

Andrew Saxton -
Tera Expert

In your code as you loop through the servers, you are telling the system to print true/false each time. Its going to do just that. I'm assuming you want it to return back if any of them are true, then this should work:

var servers = '06cb8165dbebdf444f9596a9db96190d,35efb9b6db231b00906d791c8c96198d,3defb5b6db231b00906d791c8c9619f0,e4bffdbadbeb9740b9b19c16db961934,6ad627dadb7d3704b9b19c16db961985,edb4f806db35bbc040067b668c96190e,4ffb97d6db393704b9b19c16db9619e1';
var prodServer = false;
var serverArray = servers.split(',');
    //Looping through each server in the array...
	for (var i = 0; i < serverArray.length; i++) {
		var gr = new GlideRecord('cmdb_ci_server');
                    

		if (gr.get(serverArray[i])) {

			if(gr.u_environment == 'Production'){
                gs.log('true')
                prodServer = true;
                //Telling the system to stop looping since we found a production server
                break
            }else{
                //Continue through the loop instead.
                gs.log('false')
            }
 

  
		}
                
                
	}

 

prodServer will only be true if it found a prod server, otherwise it will stay false.

 

Alternatively, you could replace the break with a return if you plan to put this code in a function, and just return true. That way if you loop through all of them without returning, you could return false since you can assume that there wasn't a production server in the list.

It works! Thank you!

 

Updated Script:

var servers = 'd52b3578db24bf8440067b668c961970,b706404edb60f640a464f209af96192d,2bb54c43db5713cc40067b668c9619c3,5e6ad77b4fff56003f1e85c98310c77d';

var serverArray = servers.split(',');

   for (var i = 0; i < serverArray.length; i++) {

        var gr = new GlideRecord('cmdb_ci_server');
                   
	    if (gr.get(serverArray[i])) {

		if(gr.u_environment == 'Production'){

                           gs.print('true');

                           break;

                 }else{
                           gs.print('false');
                 }

  
	}
                
                
}