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

Mark Stanger
Giga Sage

Try this...

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 user = new GlideRecord('sys_user');
    user.addQuery('email', emailAddress);
    user.addQuery('active', true);
    user.query();
    if (user.next()) {
        gs.info("inside");
        var grp1 = new GlideRecord('sys_user_grmember');
        grp1.user = user.getValue('sys_id');
        grp1.group = '841d4444ffa33100158bffffffffff16';
        grp1.insert();
    }
}

same problem, going infinite, its adding only one user record. I cannot even test this multiple times, because i have to cancel an active transaction, doing that too many times could get my instance stuck.

Did you find out what the issue was here?

I am also writing an after insert BR for adding one record to the table after an insert and it is going into infinite loop.

Are you trying to add a record to the same table for which the after insert Business rule is written?