How to use yes no condition for list collector in workflow

Ajay Singh2
Tera Contributor

Can anyone explain how to use list collector values in if condition in workflow for more than one selection. If I select one country from core_country table it's working fine but while selecting two or three countries based on blocked true or false values than it's going to no.

 

Thank You

1 ACCEPTED SOLUTION

Since a List Collector stores a comma-separated string of sys_ids, your gr1.AddQuery is always false when there is more than one record.  To start off with the script revisions, a Catalog Item workflow typically runs on the sc_req_item table, and you can use current.field_name or current.variables.var_name to access fields on the sc_req_item record, or variables in the Catalog Item, so you shouldn't ever need to do a GlideRecord to retrieve the current RITM record.  It's a good practice to initialize variables when you declare them, so I've added that as well for blockedcountry.  To get the List Collector value to work with the core_country GlideRecord you need to use sys_id IN the list collector value, not =.  I'm guessing you u_blocked_contry is a true/false field you've added to the core_country table, so the rest of the script accommodates that for the potentially multiple country records returned. 

 

answer = ifScript();

function ifScript() {
    var blockedcountry = false;
    //gs.log('Testing Country New'+ blockedcountry);

    var gr1 = new GlideRecord('core_country');
    gr1.addQuery('sys_id', 'IN', current.variables.select_which_country_ies_you_will_be_visiting);
    gr1.query();
    while (gr1.next()) {
		if (gr1.u_blocked_country) {
            blockedcountry = gr1.u_blocked_country; //only set the script variable to true if the field on this country table is true
            break; //no need to continue if one is found
        }
	}

    if (!blockedcontry) {
        //gs.log('No blocked countries', blockedcontry);
        return 'yes';
    } else {
        //gs.log('one or more blocked countries', blockedcontry);
        return 'no';
    }
}

 

 

 

View solution in original post

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

A List Collector variable stores the value as a comma-separated list of sys_ids for records on the List table.  So in an if statement you could use something like

if (current.variables.list_collector_name.indexOf('a sys_id') > -1) {

indexOf returns the position of the string.  If it is not found, it returns -1, so this line means - if one of the selected records on a List Collector is a specific record,... 

Ajay Singh2
Tera Contributor

Hi Brad I have written below codes and when i select two country name than it's always going to no condition but when i select single country name based on blocked country column true false its working fine but issue is when selecting more than one country than its always going to no..

 

answer = ifScript();
function ifScript() {
var gr = new GlideRecord("sc_req_item");
gr.addQuery('sys_id', current.sys_id);
gr.query();
//gr.getRowCount();
//gs.log('Count country' + gr.getRowCount());
 
if (gr.next() ) {
    var blockedcontry;
//gs.log('Testing Country New'+ blockedcontry);
 
    var gr1 = new GlideRecord('core_country');
    gr1.addQuery('sys_id', gr.variables.select_which_country_ies_you_will_be_visiting);
    gr1.query();
    if (gr1.next() ) {
         blockedcontry = gr1.u_blocked_country;
}   
 
if(!blockedcontry) {
//gs.log('Testing yes', blockedcontry);
return 'yes';
    }
 
else {
//gs.log('Testing no', blockedcontry);
return 'no';
 
}
}
}

Since a List Collector stores a comma-separated string of sys_ids, your gr1.AddQuery is always false when there is more than one record.  To start off with the script revisions, a Catalog Item workflow typically runs on the sc_req_item table, and you can use current.field_name or current.variables.var_name to access fields on the sc_req_item record, or variables in the Catalog Item, so you shouldn't ever need to do a GlideRecord to retrieve the current RITM record.  It's a good practice to initialize variables when you declare them, so I've added that as well for blockedcountry.  To get the List Collector value to work with the core_country GlideRecord you need to use sys_id IN the list collector value, not =.  I'm guessing you u_blocked_contry is a true/false field you've added to the core_country table, so the rest of the script accommodates that for the potentially multiple country records returned. 

 

answer = ifScript();

function ifScript() {
    var blockedcountry = false;
    //gs.log('Testing Country New'+ blockedcountry);

    var gr1 = new GlideRecord('core_country');
    gr1.addQuery('sys_id', 'IN', current.variables.select_which_country_ies_you_will_be_visiting);
    gr1.query();
    while (gr1.next()) {
		if (gr1.u_blocked_country) {
            blockedcountry = gr1.u_blocked_country; //only set the script variable to true if the field on this country table is true
            break; //no need to continue if one is found
        }
	}

    if (!blockedcontry) {
        //gs.log('No blocked countries', blockedcontry);
        return 'yes';
    } else {
        //gs.log('one or more blocked countries', blockedcontry);
        return 'no';
    }
}

 

 

 

Mani A
Tera Guru

@Ajay Singh2 

var ritm= new GlideRecord('sc_req_item');

if(ritm.get(current.sys_id.toString());
var x = ritm.variables.select_which_country_ies_you_will_be_visiting;

x=x.toString().split(',');

var flag = false;

for (var i = 0; i < x.length; i++) {
var gr = new GlideRecord('core_country'); 
if (gr.get(x[i])) {
if (gr.u_blocked_country== true) { 
flag = true;
break; 
}
}

answer = flag;