- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 07:22 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 08:56 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 07:33 AM
If you are doing this in a (legacy) workflow If activity, use the script syntax suggested in the activity
// This script needs to set answer to 'yes' or 'no' to indicate the state of the activity.
//
// For example,
//
// answer = ifScript();
//
// function ifScript() {
// if (condition is true) {
// return 'yes';
// }
// return 'no';
// }
If this is a List Collector type variable in a Catalog Item, be sure to use the syntax current.variables.var_name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 08:00 AM
Hi @Atchutaram ,
You should not hardcode the sysIDs. You can write the code as below:
var services = workflow.inputs.services;
var mailbxx = false;
if(services){
var ids = services.split(',');
var gr = new GlideRecord("SERVICE TABLE");
gr.addQuery('sys_id', 'IN', ids);
gr.query();
while(gr.next()){
if(gr.name == 'mailbox' || gr.name == 'EventQueue' ){
mailbxx = true;
}
}
}
answer = mailbxx ;
Please REPLACE gr.name with the appropriate field from SERVICE table.(could be u_name or name etc.);
Regards,
Mohammad Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 08:50 AM
Hi @Atchutaram,
You can write a script like below in your workflow script:
answer = serviceContainsMailbox("Mailbox");
function serviceContainsMailbox(displayName) {
var selected = current.variables.u_services.toString(); // CSV sys_ids
if (!selected) return 'no';
var gr = new GlideRecord('services'); // The referenced table
gr.addQuery('sys_id', 'IN', selected);
gr.addQuery('name', displayName); // Match the display value
gr.query();
return gr.hasNext() ? 'yes' : 'no';
}
Best Regards,
Sharif
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 08:56 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader