Issue with workflow condition based on a list collector variable

Sue143
Tera Expert

Hi all,

 

I am trying to get an yes or no answer on my workflow based on a list collector values, which are reference from a custom table.

What I need to achieve is to identify the values selected in the list collector and based on these values do something like dot walking to get which change group is related to that value in the table.

This is my script, but so far does not return any value, can someone help me and put me in the right direction?

 

answer = ifScript();
 
function ifScript() {
    var collector = current.variables.my_variable.toString(); //get variable name from catalog item
    var list = collector.split(",");
    for (var i = 0; i < list.length; i++) {
        var gr = new GlideRecord('my_custom_table'); //access the custom table that store the values from List collector
        gr.addQuery('u_change_group=dcfb023edb4d1340439d14a05b9619c5'); //define the change group with sys_ID
        if (gr.get(list[i]))
            return 'yes';

        else

            return 'no';

    }
}
1 ACCEPTED SOLUTION

The next step is to temporarily add some log lines to the script so you can see what is happening.  It also sounds like this would be better with a more conventional approach - return all of the list collector records, then test each for the condition match.  Also, I shouldn't have used 'answer' as a script variable since it is already a part of the if activity.

 

 

answer = ifScript();
 
function ifScript() {
    var collector = current.variables.my_variable.toString(); //get variable name from catalog item
    workflow.info('collector = ' + collector); //verify the variable was retrieved
    var gr = new GlideRecord('my_custom_table'); //access the custom table that store the values from List collector
    gr.addQuery('sys_id', 'IN', collector);
    gr.query();
    while (gr.next()) {    
        workflow.info('Record found ' + gr.sys_id + ' Group = ' + gr.u_change_group); //change this to a more descriptive field on the custom table
        if (gr.getValue('u_change_group') == 'dcfb023edb4d1340439d14a05b9619c5') { //define the change group with sys_ID
            workflow.info('Group matched');            
            return 'yes'; //stop running the script when the first group match is found
        }
    }
    workflow.info('No list collector record had a matching group');    
    return 'no'; //if no groups matched, take the No path
}

 

 

Be sure to test this workflow change with a newly-submitted record.  You can view these logs on the Log tab while viewing the context from the Related Link at the bottom of the RITM.

  

View solution in original post

10 REPLIES 10

Hi Brad, thank you for your response.

 

I have tried your code but is still not working.

The workflow does not end up in an error, but the if condition returns true every time, even when it shouldn't.

 

I am trying to loop through, not all, but only the values selected by the user from the list collector and decide with an if condition statement if these selected values have as change group the one defined on the code:

('u_change_group', 'dcfb023edb4d1340439d14a05b9619c5')

and if not, the answer to the if statement should be no, but this is not happening in the moment.

 

the workflow I am building depends on this if condition to create a task and assign to this change group in case it is "true". Otherwise, this should be skipped. But right now it is creating the task even when the change group of the value select is other.

 

The next step is to temporarily add some log lines to the script so you can see what is happening.  It also sounds like this would be better with a more conventional approach - return all of the list collector records, then test each for the condition match.  Also, I shouldn't have used 'answer' as a script variable since it is already a part of the if activity.

 

 

answer = ifScript();
 
function ifScript() {
    var collector = current.variables.my_variable.toString(); //get variable name from catalog item
    workflow.info('collector = ' + collector); //verify the variable was retrieved
    var gr = new GlideRecord('my_custom_table'); //access the custom table that store the values from List collector
    gr.addQuery('sys_id', 'IN', collector);
    gr.query();
    while (gr.next()) {    
        workflow.info('Record found ' + gr.sys_id + ' Group = ' + gr.u_change_group); //change this to a more descriptive field on the custom table
        if (gr.getValue('u_change_group') == 'dcfb023edb4d1340439d14a05b9619c5') { //define the change group with sys_ID
            workflow.info('Group matched');            
            return 'yes'; //stop running the script when the first group match is found
        }
    }
    workflow.info('No list collector record had a matching group');    
    return 'no'; //if no groups matched, take the No path
}

 

 

Be sure to test this workflow change with a newly-submitted record.  You can view these logs on the Log tab while viewing the context from the Related Link at the bottom of the RITM.

  

Hi Brad,

 

Thank you so much for the time you taking to help me, but it still not working.

The logs shows that the code works fine to find a match for the collector on the table, but it also say "group matched" for all the 3 if conditions on my workflow:

suellen2_0-1724434188936.png

each if condition matches a different change group (different sys_id), so it shouldn't match all unless I it is selected multiple values in the list collector, which I haven't for testing purposes.

suellen2_1-1724434371832.png


this is the log results, each record (role) only have a change group, so it should return true for only one of the if conditions

So just to confirm, you are using the same script in all three if activities, only changing this line to a different sys_id?

        if (gr.getValue('u_change_group') == 'dcfb023edb4d1340439d14a05b9619c5'); //define the change group with sys_ID

I just noticed I'm missing a { on this line - I just corrected it in the full script above, and added group to the log.  Not sure if that accounts for this behaviour, or if you previously corrected this. 

Thank you Brad!!!!!

I actually didn't noticed either, after correcting the missing comma the code works.

and yes, I am using the same script for all the 3 if conditions just modifying the sys_id to match each change group.