In Workflow , I am trying to put an If Condition , but it's not working as per expectation

sonalpriyanka95
Kilo Expert

Hi Team ,

I am trying to imply an workflow where it should runs events when , it met conditions like Lost item is either mifi device, byod device, issued mobile or issued surface device , or all of them or any of these combination basically , so however , In this case I tried both the ways , it's not working , the condition works when only one of these option is selected but when more than 1 option selected workflow doesn't work .

 

Please Suggest me further how I can achieve it .

1 ACCEPTED SOLUTION

@sonalpriyanka95 

please share script here and not screenshot.

did you give correct variable name and correct record sysIds?

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

View solution in original post

6 REPLIES 6

@sonalpriyanka95 

Glad to know that my script worked.

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

Rafael Batistot
Tera Sage

Hi @sonalpriyanka95 

 

Based in your screenshots I can’t see the item, it’s like “select item” 

 

My you try your if condition via script 

 

Selected true in “advanced” box and do some like this:

 

 

answer = checkVar();

function checkVar() {
// Get the variable value as a string (ServiceNow list collector may return a comma-separated string)
var variableValue = current.variables.VariableName; // e.g., "option_1,option_3,option_5"

if (!variableValue) {
return "no";
}

// Split the string into an array
var selectedOptions = variableValue.split(',');

// Initialize flags
var hasOption1 = false;
var hasOption5 = false;

// Loop through each selected option
selectedOptions.forEach(function(option) {
if (option.trim() === "option_1") {
hasOption1 = true;
} else if (option.trim() === "option_5") {
hasOption5 = true;
}
});

// Return "yes" only if both option_1 and option_5 are selected
if (hasOption1 && hasOption5) {
return "yes";
} else {
return "no";
}
}

}