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

MrMuhammad
Giga Sage

you can use ArrayUtil class function intersect() to achieve this.

var arr = new ArrayUtil();

var arrInter = arr.intersect(createCaseRoles, my_roles); //pass arrays as a paramter.

var count = arrInter.length; //gives the count of common values
Regards,
Muhammad

Quick test results in background script

SCRIPT

find_real_file.png

OUTPUT

find_real_file.png

Regards,
Muhammad

Milan13
Giga Expert

Thanks a lot, it works in background - script, but not within script include, once calling it (new XXX().YYY()) from scripts - background

Hi Milan,

please share your script

If you are having that script include created in custom scope application then you need to use this

var arr = new global.ArrayUtil();

Regards
Ankur

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