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

Brad Bowman
Kilo Patron
Kilo Patron

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

Mohammad Danis3
Tera Expert

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


Masthan Sharif
Tera Guru

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

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