Adding the watch list to an array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 02:10 AM
Morning All,
We have a script running on our change form that is used to send notifications to all customers that have a service offering that is dependent on the CI on the change.
I've included the script below, it is basically pulling out all the customers and getting rid of duplicates and then acquiring all their service offerings and presenting these as parm1 and parm2 respectively.
I want to add in the watch list to the list of recipients in parm1 but i don't know how to add them to the array. I've tried various permutations along the lines of
var custArray = firstArray.push(current.watch_list);
and
var combArray = new ArrayUtil();
var firstArray = combArray.concat(custArray, current.watch_list);
and even
var newArray = new ArrayUtil();
var watchArray = newArray.convertArray(current.watch_list);
var combArray = new ArrayUtil();
var firstArray = combArray.concat(customers, watchArray);
Please help me!!
(function executeRule(current, previous /*null when async*/) {
var customers;
var count = 0;
//get all affected service offerings
var gr = new GlideRecord("task_cmdb_ci_service");
gr.addQuery("task", current.sys_id);
gr.query();
while (gr.next()) {
//get all affected customers
if(count == 0){
customers += gr.u_company;
}
else {
customers += "," + gr.u_company;
}
count++;
}
//remove duplicate customers
var custArray = customers.split(",");
var arrayUtil = new ArrayUtil();
var uniqueCustArray = arrayUtil.unique(custArray);
//trigger notification to all affected customers
for (var i=0; i<uniqueCustArray.length; i++) {
var serviceOfferings = "";
var emailAddress = "";
var count1 = 0;
//get all service offerings for affected customer
var gr1 = new GlideRecord("task_cmdb_ci_service");
gr1.addQuery("task", current.sys_id);
gr1.addQuery("u_company", uniqueCustArray[i]);
gr1.query();
while (gr1.next()) {
if(count1 == 0){
serviceOfferings += gr1.cmdb_ci_service.u_service_id;
}
else {
serviceOfferings += ", " + gr1.cmdb_ci_service.u_service_id;
}
count1++;
}
//get email addresses of affected customers
var gr2 = new GlideRecord("core_company");
if (gr2.get(uniqueCustArray[i])) {
emailAddress = gr2.u_primary_contact;
}
//trigger event
gs.eventQueue("change.customer.notification.reminder", current, emailAddress, serviceOfferings);
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 02:23 AM
Hi David,
You can convert the watchlist to array by below lines.
var arr=[];
var inc=new GlideRecord('incident');
inc.addQuery('number','<any number>');
inc.query();
if(inc.next())
{
arr=inc.watch_list.split(',');
gs.print(arr[0]);
}
Hope this helps.
Regards
Ujjawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 03:14 AM
Hi Ujjawal,
Thanks for the response. If i'm running this script in a UI action or a business rule can i not use current instead of a glide record?
eg:
var arr = [];
arr.push(current.watch_list);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 03:20 AM
Yes, you can. current is an object of the gliderecord for the current form and you can use it in server side( BR, UI action).
Hope this helps.
Regards
Ujjawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 04:03 AM
Thanks for confirming that Ujjawal.
Once i've created the array how do i join it with the customers array? I've tried array.concat(array1, array2) but it's not working as expected!