The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Loop iterating only once

VNK1216
Tera Guru

Hello Experts,

I'm trying to automate conf calls on creation of a major incident.

I have added script to calls the members in the watch list. The issue here is that the for loop is iterating only once and not fetching the other members in the watch list.

Business Rule:

(function executeRule(current, previous /*null when async*/) {	
		var conf = new AutoConference();
		conf.openPopUpConference(current.incident.number);
	
})(current, previous);

 

Scirpt Include:

var AutoConference = Class.create();
AutoConference.prototype = {
	initialize: function() {
	},
	
	openPopUpConference : function (tt){
		var callParticipantNumbers = [];
		
		var notify = new SNC.Notify();
		var from = '+14848804832';
		var watchlist;
		
		var grTask = new GlideRecord('incident');
		grTask.addQuery('number',tt);
		grTask.query();
		if(grTask.next()){
			watchlist = grTask.watch_list.getDisplayValue();
		}
		var participants = watchlist; //field where participants are stored.
		participants = participants.split(",");
		
		for (var i in participants) {
			var grMem = new GlideRecord('sys_user');
			grMem.addQuery('name',participants[i].toString());
			grMem.query();
			if(grMem.next()){
				
				var phn = grMem.mobile_phone.toString();
				callParticipantNumbers.push(phn);
			}
		}
		
		var conferenceCall = notify.conferenceCall();
		for (var k in callParticipantNumbers) {
			var ton = callParticipantNumbers[k];
			notify.call(from, ton, conferenceCall);
		}
		
		gs.print(gs.getMessage('set up a conference call with number {0} and (re)join code: {1}',
			[ conferenceCall.getValue('number'), conferenceCall.getValue('code') ]));
			
			gs.log(gs.getMessage('set up a conference call with number {0} and (re)join code: {1}',
				[ conferenceCall.getValue('number'), conferenceCall.getValue('code') ]));
				
			},
			type: 'AutoConference'
		};

 

 

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

A couple key things to understand on this script.

First, don't pass the number of the incident. Display values (like number) are often NOT unique. You could have two incident #10000 and your script is going to use whatever it finds first. Instead pass the sys_id like this:

conf.openPopUpConference(current.getValue('incident'));

Secondly, retrieve the value of the watch list and recognize it as a comma separated list of sys_ids. from there you can retrieve the records. You're getting a comma separated list of names right now.

Do the same type of retrieval from sys_user using the sys_ids. These are unique and can be used like this:

var watch_list = incGr.getValue('watch_list');
var participants = watch_list.spli(',');

for (var i = 0; i < participants.length; i++) {
   var usrGr = new GlideRecord('sys_user');
   if (usrGr.get(particpants[i])) {
       // do stuff on the usrGr record here
   }
}

View solution in original post

4 REPLIES 4

Tim Deniston
Mega Sage
Mega Sage

Can you put "gs.print('callParticipantNumbers: ' + callParticipantNumbers);" right before you declare "var conferenceCall"? I'm curious if there are a bunch of the same numbers in callParticipantNumbers or just one entry. 

Chuck Tomasi
Tera Patron

A couple key things to understand on this script.

First, don't pass the number of the incident. Display values (like number) are often NOT unique. You could have two incident #10000 and your script is going to use whatever it finds first. Instead pass the sys_id like this:

conf.openPopUpConference(current.getValue('incident'));

Secondly, retrieve the value of the watch list and recognize it as a comma separated list of sys_ids. from there you can retrieve the records. You're getting a comma separated list of names right now.

Do the same type of retrieval from sys_user using the sys_ids. These are unique and can be used like this:

var watch_list = incGr.getValue('watch_list');
var participants = watch_list.spli(',');

for (var i = 0; i < participants.length; i++) {
   var usrGr = new GlideRecord('sys_user');
   if (usrGr.get(particpants[i])) {
       // do stuff on the usrGr record here
   }
}

VNK1216
Tera Guru

Thank you Chuck!!

It worked for me.

You're welcome. Happy to help.

FWIW, this was featured on the Community Live Stream on June 26, 2018 (at about 38:00).