if condition for a list collector field in workflow

Atchutaram
Tera Contributor

Hi Everyone.

I have a requirement where there is a field called "services", and the type of field is list collector, i have given services table in that variable. there are two option: 1) mailbox 2) event queue. we have workflow, in which i used this list collector in "IF" block. this if should generate yes if "mailbox" is selected, it should also generate yes, if both the options are selected. How can i write a code for this.

var select = current.u_services.toString();
var selectArray = select.split(',');
var opt1 = selectArray.indexOf('3460adbfdb7e1b00dd1662eb0b96198a') !== -1;
var opt2 = selectArray.indexOf('3860adbfdb7e1b00dd1662eb0b96198b') !== -1;
if (opt1) {
    answer = true;

} else {
    answer = false;
}
 
I have given sys id of those two options.
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Atchutaram 

if you are using IF activity then it has a syntax and the answer variable should be set with yes/no

update your script as this

answer = ifScript();

function ifScript() {
    var select = current.variables.u_services.toString();
    var selectArray = select.split(',');
    var opt1 = selectArray.indexOf('3460adbfdb7e1b00dd1662eb0b96198a') !== -1;
    var opt2 = selectArray.indexOf('3860adbfdb7e1b00dd1662eb0b96198b') !== -1;
    if (opt1) {
        return 'yes';
    } else {
        return 'no';
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

nemamuskan
Tera Contributor

Hello @Atchutaram 

 

Please find the below script which I tried and tested for the requirement and it works for me.

Copy the Script as it is and it would work!!

 

answer = ifScript();

function ifScript() {
    
    var select = current.variables.u_services.toString();
    var selectArray = select.split(',');
       
    var opt1 = (selectArray.indexOf('3460adbfdb7e1b00dd1662eb0b96198a') !== -1); //mailbox

var opt2 = (selectArray.indexOf('3860adbfdb7e1b00dd1662eb0b96198b') !== -1); //event queue

        if (opt1||(opt1 && opt2)) {
                return 'yes';
    } else {
                return 'no';
    }
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Thanks and Regards,

Muskan Nema

Hello @Atchutaram,

 

Greetings of the day!!

 

Just wanted to check if my reply answer was helpful to you and answers your question?

If my response helped, please mark it helpful and close the thread so that it benefits future readers.

Thanks, and Regards,

Muskan Nema

Mohammad Danis3
Tera Expert

@Atchutaram ,

I hope you are doing well!

Did my response help you? 
Please let me know if you stuck somewhere.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Mohammad Danish