- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-15-2018 11:59 PM
Hello,
I am not sure any this is already mentioned on community somewhere or not..But I would like to mention this some observation that I observed while working on requirements for 1 of my customer.
I was working on requirement where I had to create a record(say Alert) whenever an incident is created in ServiceNow and while creating alert I had to select users/groups and send that record to 3rd party tools. So we were doing testing by selecting a 2 groups each with 10K users. While sending the data from ServiceNow to 3rd Party tool I have to get the email Id for all the users present in Groups, because the email id was the unique field for users in 3rd party tool. So I used the below code to get the email ID of the users.
I used ArrayUtil.contains() to store the unique email id since same user can be present in more than 1 more.
(function () {
var userCountJson = {"usersCount": 0, "groupUsercount": 0, "userArr": ""};
try {
var MID_FIELD = 'email';
var users = gs.getProperty("users.sys.id");
var userArr = [];
var usersCount = 0, groupUsercount = 0;
var arrayUtil = new global.ArrayUtil();
if(users) {
var gdt = new GlideDateTime();
gs.log("GDT : " +gdt);
gs.print(gdt.getNumericValue());
var userRef = new GlideRecord('sys_user');
userRef.addEncodedQuery('active=true^sys_idIN' + users);
userRef.query();
while (userRef.next()) {
//STORE THE USER EMAIL ID IN ARRAY AS USER CAN BE PRESENT IN MORE THAN 1 GROUP
if(arrayUtil.contains(userArr, userRef.getValue(MID_FIELD))) {
continue;
}
else {
if (userRef.getValue(MID_FIELD)) {
userArr.push(userRef.getValue(MID_FIELD));
usersCount++;
}
}
}
}
var gdt1 = new GlideDateTime();
gs.log("GDT : " +gdt1);
gs.print(gdt1.getNumericValue());
userCountJson.usersCount = usersCount;
userCountJson.groupUsercount = groupUsercount;
userCountJson.userArr = userArr.join(",");
} catch(error) {
gs.info('getUsersCount --> error : ' + error, this.type);
}
gs.info("USER COUNT JSON : " + global.JSON.stringify(userCountJson));
})();
The above code was taking around 25 seconds to get the unique email ID of the users as shown below:
1st run result:
Time taken in seconds = End time (in ms) - Start time (in ms)
Time taken in seconds = 1542275656621 - 1542275631536
Time taken in seconds = 25085 ms = 25.08 seconds
2nd run result:
Time taken in seconds = 1542275850186 - 1542275824726
Time taken in seconds = 25460 ms = 25.46 seconds
So I had to optimize this solution and I thought of using JSON object to store the email ID as JSON keys are always unique. I store the email ID in JSON object as shown below:
var emailIdJson = {"user1_email_id": "y", "user2_email_id": "y"....}
var emailIdJson = {"abel.tuter@example.com": "y", "james@example.com": "y"...}
Below is the updated code after making the changes to store the email ID in JSON object:
(function () {
var userCountJson = {"usersCount": 0, "groupUsercount": 0, "userArr": ""};
try {
var jsonData = {};
var userArr = [];
var usersCount = 0;
var groupUsersCount = 0;
var tempKey;
var users = gs.getProperty('users.sys.id');
var MID_FIELD = 'email';
//for users in Target Users
if(users) {
var gdt = new GlideDateTime();
gs.log("GDT : " +gdt);
gs.log(gdt.getNumericValue());
var userRef = new GlideRecord('sys_user');
userRef.addEncodedQuery('active=true^sys_idIN' + users);
userRef.query();
while (userRef.next()) {
tempKey = userRef.getValue(MID_FIELD);
if(tempKey)
{
if (!jsonData[tempKey]) {
jsonData[tempKey] = 'Y';
usersCount++;
}
}
}
}
var gdt1 = new GlideDateTime();
gs.log("GDT : " +gdt1);
gs.log(gdt1.getNumericValue());
userCountJson.usersCount = usersCount;
userCountJson.groupUsercount = groupUsersCount;
for(var x in jsonData)
userArr.push(x);
userCountJson.userArr = userArr.join(",");
}
catch (error) {
gs.info("getUsersCountJson --> error : " + error);
}
gs.log("USER COUNT JSON : " +JSON.stringify(userCountJson));
//return userCountJson;
})();
The updated code was taking around 1.5 seconds to get store the unique email ID and below is the result:
1st Run Result:
Time taken in seconds = End time (in ms) - Start time (in ms)
Time taken in seconds =1542275130122 - 1542275128709
Time taken in seconds = 1413 ms = 1.4 seconds
2nd Run Result:
Time taken in seconds = 1542275242466 - 1542275241110
Time taken in seconds =1356 ms = 1.35 seconds
So I would suggest that if you have to save unique value in your integration then you can store the data in JSON object instead of storing in unique data in array using ArrayUtil.contains(). I have used ArrayUtil.contains() in past in Integration/scripts and I think most of the people uses ArrayUtil.contains for the above mentioned scenarios so thought of sharing this observation.
Mark it helpful if it helps you to optimize your script and reduce the time taken by script to process the records.
Thanks.
- 793 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Have you tried simply pushing values without using arrayUtil.contains() and at the end using arrayUtil.unique() to get an array of unique values? That is usually how I'm doing it when I need unique values. I don't think it will be as fast as your key value object but it should be close.