The CreatorCon Call for Content is officially open! Get started here.

Looping through MRVS and one of it's look up select box Variable to create task in workflow

mohanreshma
Tera Contributor

I have a look up select box variable in MRVS for selecting multiple countries at a time. I'm trying to create task in workflow according to some condition 

  1.  if user select country  Cuba||Canada in the MRVS the catalog task should not create
  2. if user select both Cuba && Canada only, then also catalog task should not create
  3.  But, if user select any other country along with Cuba or Canada then Catalog task should create 

 eg : country = Cuba and canada-> No Catalog Task

       country = Cuba --> No Catalog task

       country = India ---> Create Catalog Task

       country = India,Cuba --> Create Catalog task

 

How to achieve this in servicenow workflow

 

I have written a if condition code but it is only checking the first row .how i will loop through all the country selected and check the condition?

 

answer = ifScript();

function ifScript() {
    var result = '';
    var mrvsRows =current.variables.low;
//The above will return a string in the form of array and objects. Now you will have to parse it

var oMrvs = JSON.parse(mrvsRows);
for(var i in oMrvs) {
    if(oMrvs[i].low_country === 'Cuba' || oMrvs[i].low_country === 'Canada'){
           result = 'no';

    }
    else if(oMrvs[i].low_country === 'Cuba' && oMrvs[i].low_country === 'Canada'){

          result = 'no';
    }
    else{
         result = 'yes';
    }
}
return result;
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@mohanreshma 

use this

answer = ifScript();

function ifScript() {
    var result = 'yes'; // Default to 'yes'
    var mrvsRows = current.variables.low;
    var oMrvs = JSON.parse(mrvsRows);
    var hasCuba = false;
    var hasCanada = false;
    var otherCountrySelected = false;

    for (var i in oMrvs) {
        if (oMrvs[i].low_country === 'Cuba') {
            hasCuba = true;
        } else if (oMrvs[i].low_country === 'Canada') {
            hasCanada = true;
        } else {
            otherCountrySelected = true;
        }
    }

    if ((hasCuba && hasCanada && !otherCountrySelected) || (hasCuba && !otherCountrySelected) || (hasCanada && !otherCountrySelected)) {
        result = 'no';
    }

    return result;
}

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

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@mohanreshma 

use this

answer = ifScript();

function ifScript() {
    var result = 'yes'; // Default to 'yes'
    var mrvsRows = current.variables.low;
    var oMrvs = JSON.parse(mrvsRows);
    var hasCuba = false;
    var hasCanada = false;
    var otherCountrySelected = false;

    for (var i in oMrvs) {
        if (oMrvs[i].low_country === 'Cuba') {
            hasCuba = true;
        } else if (oMrvs[i].low_country === 'Canada') {
            hasCanada = true;
        } else {
            otherCountrySelected = true;
        }
    }

    if ((hasCuba && hasCanada && !otherCountrySelected) || (hasCuba && !otherCountrySelected) || (hasCanada && !otherCountrySelected)) {
        result = 'no';
    }

    return result;
}

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

Thank you Ankur!!!! It is working