How to find an intersection of array data in a script include

Milan13
Giga Expert

Hello,

I have two arrays and need to find  count of records which these arrays share in common.

Need to place this in a script include and make this as simple as possible.

The array are:

var createCaseRoles = ['sn_customerservice.customer', 'sn_customerservice.customer_case_manager', 'sn_customerservice.customer_admin', 'sn_customerservice.partner', 'sn_customerservice.partner_admin', 'sn_customerservice.consumer', 'sn_customerservice_agent', 'sn_customerservice_manager', 'sn_customerservice.consumer_agent', 'sn_customerservice_manager'];

var my_roles =['sn_customerservice.customer_admin', 'sn_customerservice.partner_admin', 'sn_customerservice.customer_case_manager' , 'snc_internal' , 'admin' , 'sn_customerservice.customer', 'sn_customerservice.partner' ,'security_admin'];

Any idea how to make this as simple as possible with ossible use inside scriprt include, need to call a method in scriot include returning count of records these array share in common.

Appreciate your help,

Milan

 

18 REPLIES 18

Milan13
Giga Expert

This is within script inlcude:

 

checkUserRoles: function() {
var my_roles = [];
my_roles = gs.getUser().getUserRoles();


var createCaseRoles = ['sn_customerservice.customer', 'sn_customerservice.customer_case_manager', 'sn_customerservice.customer_admin', 'sn_customerservice.partner', 'sn_customerservice.partner_admin', 'sn_customerservice.consumer', 'sn_customerservice_agent', 'sn_customerservice_manager', 'sn_customerservice.consumer_agent', 'sn_customerservice_manager'];

 

var intersectRoles = new global.ArrayUtil().intersect(my_roles, createCaseRoles);


var count = intersectRoles.length;
return intersectRoles;


},

 

this is how I call it in background-scripts and get no results even if the code itself works withing background-scripts

var x = new CsmUtils().checkUserRoles()

gs.print(x

 

 

 

 

 

Try below and call it from background script. Let me what you get in response to logs

checkUserRoles: function() {
var my_roles;
my_roles = gs.getUser().getUserRoles();

var createCaseRoles = ['sn_customerservice.customer', 'sn_customerservice.customer_case_manager', 'sn_customerservice.customer_admin', 'sn_customerservice.partner', 'sn_customerservice.partner_admin', 'sn_customerservice.consumer', 'sn_customerservice_agent', 'sn_customerservice_manager', 'sn_customerservice.consumer_agent', 'sn_customerservice_manager'];
var intersectRoles = new global.ArrayUtil().intersect(my_roles, createCaseRoles);
gs.info("ROLES --> " + intersectRoles);

var count = intersectRoles.length;
gs.info("COUNT --> "+count);
return count;


},

If you are running background script in global scope then call script include like this

var x = new API_NAME_HERE().checkUserRoles()

gs.print(x);

Regards,
Muhammad

Hi Milan,

please try this

checkUserRoles: function() {
var my_roles = [];
my_roles = gs.getUser().getUserRoles().toString().split(',');


var createCaseRoles = ['sn_customerservice.customer', 'sn_customerservice.customer_case_manager', 'sn_customerservice.customer_admin', 'sn_customerservice.partner', 'sn_customerservice.partner_admin', 'sn_customerservice.consumer', 'sn_customerservice_agent', 'sn_customerservice_manager', 'sn_customerservice.consumer_agent', 'sn_customerservice_manager'];

var intersectRoles = new global.ArrayUtil().intersect(my_roles, createCaseRoles);

var count = intersectRoles.length;

return intersectRoles;

},

Background Script:

var x = new CsmUtils().checkUserRoles();

gs.print(x);

Regards
Ankur

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

getUserRoles() returns an array of a string so I don't think we need to convert that to string and then convert back to the array. Please correct me if I am wrong.

Regards,
Muhammad

Hi,

It returns type as an "object" and hence we need to convert it as an array.

Please try this one in your background script.

var my_roles = gs.getUser().getUserRoles();
gs.print(typeof my_roles);