If script to check for the selected role from an Array

Girish Heble
Tera Contributor

Hi,

 

I have an item where we have a field named Please choose the role which is a List collector field referring to a custom table with 127 roles in total however, I have a requirement to create a new task if only the request is for any of the listed 26 roles. I have tried a script with some help but it is not working. Could anyone please help in this regard?

 

answer = ifScript();
//
var roles = "Buyer,Capacity Manager,Commodity Specialist,Customer Service Specialist,Customer Service Supervisor,Executive Inquiry,Global Commodity Leader,Global Planning Director,Make Planning Admin,Make Planning Inquiry,Manufacturing Admin,Manufacturing Engineer,Manufacturing Engineering Manager,Manufacturing Inquiry,Manufacturing Manager,Master Scheduler,Material Planner,Order Management Admin,Planning Manager,Planning Supervisor,Production Planner,Purchasing Admin,Purchasing Manager (for IT admins only),Supply Chain Planner,Supply Chain Planning Admin,Supply Chain Planning Inquiry";

var arr_roles = roles.split(',');

function ifScript() {
var arr = current.variables.r12_roles.getDisplayValue().split(',');
for (i = 0; i < arr.length; i++)
if (inArray(arr[i].trim(), arr_roles)) {
return 'yes';
} else {
return 'no';
}

}

function inArray(value, arr_roles) {
var con = arr_roles.indexOf(value);
var status = false;
if (con > -1) {
status = true;
}
return status;
}

1 ACCEPTED SOLUTION

Harshad Wagh
Tera Guru

can you pls try this?

 

answer = ifScript();
//




function ifScript() {

var roles = "Buyer,Capacity Manager,Commodity Specialist,Customer Service Specialist,Customer Service Supervisor,Executive Inquiry,Global Commodity Leader,Global Planning Director,Make Planning Admin,Make Planning Inquiry,Manufacturing Admin,Manufacturing Engineer,Manufacturing Engineering Manager,Manufacturing Inquiry,Manufacturing Manager,Master Scheduler,Material Planner,Order Management Admin,Planning Manager,Planning Supervisor,Production Planner,Purchasing Admin,Purchasing Manager (for IT admins only),Supply Chain Planner,Supply Chain Planning Admin,Supply Chain Planning Inquiry";
var arr_roles = roles.toString().split(',');
var arr = current.variables.r12_roles.getDisplayValue().split(',');
for (i = 0; i < arr.length; i++)
if (inArray(arr[i].trim(), arr_roles)) {
return 'yes';
} else {
return 'no';
}

}

function inArray(value, arr_roles) {
var con = arr_roles.toString().indexOf(value);
var status = false;
if (con > -1) {
status = true;
}
return status;
}

View solution in original post

4 REPLIES 4

Vishal Birajdar
Giga Sage

Hi @Girish Heble ,

 

As you said variable is List collector, can you put some logs and check if what value you are getting for below line :

 

 

var arr = current.variables.r12_roles.getDisplayValue().split(',');  // sys_ids or names of roles ...??

 

 

What I think is ,

Instead of Name of roles you should provide sys_id's of role in below variable :

 

var roles = "Buyer,Capacity Manager,Commodity Specialist,Customer Service Specialist,Customer Service Supervisor,Executive Inquiry,Global Commodity Leader,Global Planning Director,Make Planning Admin,Make Planning Inquiry,Manufacturing Admin,Manufacturing Engineer,Manufacturing Engineering Manager,Manufacturing Inquiry,Manufacturing Manager,Master Scheduler,Material Planner,Order Management Admin,Planning Manager,Planning Supervisor,Production Planner,Purchasing Admin,Purchasing Manager (for IT admins only),Supply Chain Planner,Supply Chain Planning Admin,Supply Chain Planning Inquiry"; 

 

                                                                        

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Harshad Wagh
Tera Guru

can you pls try this?

 

answer = ifScript();
//




function ifScript() {

var roles = "Buyer,Capacity Manager,Commodity Specialist,Customer Service Specialist,Customer Service Supervisor,Executive Inquiry,Global Commodity Leader,Global Planning Director,Make Planning Admin,Make Planning Inquiry,Manufacturing Admin,Manufacturing Engineer,Manufacturing Engineering Manager,Manufacturing Inquiry,Manufacturing Manager,Master Scheduler,Material Planner,Order Management Admin,Planning Manager,Planning Supervisor,Production Planner,Purchasing Admin,Purchasing Manager (for IT admins only),Supply Chain Planner,Supply Chain Planning Admin,Supply Chain Planning Inquiry";
var arr_roles = roles.toString().split(',');
var arr = current.variables.r12_roles.getDisplayValue().split(',');
for (i = 0; i < arr.length; i++)
if (inArray(arr[i].trim(), arr_roles)) {
return 'yes';
} else {
return 'no';
}

}

function inArray(value, arr_roles) {
var con = arr_roles.toString().indexOf(value);
var status = false;
if (con > -1) {
status = true;
}
return status;
}

Thank you so much! It worked as per the expectation.

Ankur Bawiskar
Tera Patron
Tera Patron

@Girish Heble 

why not use ArrayUtil class and the intersect method?

If there is any common role from the 26 list in your list collector then return true

ArrayUtil - Global 

Please store the role names in system property rather than hard-coding. This will help in future if a new role is added/removed as per requirement

answer = ifScript();

function ifScript() {

	var roles = "Buyer,Capacity Manager,Commodity Specialist,Customer Service Specialist,Customer Service Supervisor,Executive Inquiry,Global Commodity Leader,Global Planning Director,Make Planning Admin,Make Planning Inquiry,Manufacturing Admin,Manufacturing Engineer,Manufacturing Engineering Manager,Manufacturing Inquiry,Manufacturing Manager,Master Scheduler,Material Planner,Order Management Admin,Planning Manager,Planning Supervisor,Production Planner,Purchasing Admin,Purchasing Manager (for IT admins only),Supply Chain Planner,Supply Chain Planning Admin,Supply Chain Planning Inquiry";
	
	// var roles = gs.getProperty('propertyName'); // store the role names as comma separated values

	var arr_roles = roles.split(',');
	var arr = current.variables.r12_roles.getDisplayValue().split(',');

	var arrayUtil = new ArrayUtil();
	var intersectionArray = arrayUtil.intersect(arr_roles, arr);

	if(intersectionArray.length > 0)
		return true;
	else
		return false;
}

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