Script going into infinite loop

Nitesh Balusu
Giga Guru

Hello,

the follow script is going to infinite loop and adding only one user record infinite times into sys_user_grmember table. When i print logs of the user record instead of inserting the record, its printing all the records that i need. am i missing something? The number of email ID's in var=a are about 170, for hiding email addresses i just posted two of them.

I am puling the email from the bracket, comparing it with user records and then if found, adding that user to a group. Very straight forward, not sure why its going into infinite.

 

var a='abc, cdf <123@gmail.com>; qwe, xyz <456@gmail.com>';
var split1 = a.split(";");
var usersysids= [];
for(var i=0;i<split1.length;i++){
var initialString = split1[i];
var emailAddress  = initialString.substring(initialString.indexOf("<") + 1, initialString.indexOf(">"));
gs.info('Email Address Array is: ' + emailAddress);
var arr=[];
var user=new GlideRecord('sys_user');
	user.addQuery('email',emailAddress);
	user.addQuery('active',true);
	user.query();
	while(user.next())
		{
			gs.info("inside");
	usersysids[i]=user.sys_id;
			gs.info(usersysids[i]);
			var grp1=new GlideRecord('sys_user_grmember');
			grp1.user=usersysids[i];
 		grp1.group='841d4444ffa33100158bffffffffff16';
 	grp1.insert();
				
		}
}
5 REPLIES 5

Not really, I got sick of trying this, I am pretty sure I am missing something very small and stupid.

I was writing a fix script.