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

@Ajay Singh2 check above code and mark accepted as solution if it is helpful

@Ajay Singh2  did you check above logic and it will work. 

@Mani A while the logic may 'work' - aside from returning boolean true/false when 'yes' or 'no' is needed in an If activity - this approach is not the most efficient.  For example, you can get to London from New York through Tokyo, but it's not the best route. 

 

If there are 10 countries selected, and only the last one retrieved is blocked, then this approach will execute 11 GlideRecord queries, when only one is needed.  The performance impact in this case may turn out to be negligible, but applying the same approach out of habit or copying/pasting in different use cases could have disastrous effects, so it's best to be in the habit of developing scripts that are as efficient as possible.