Script going into infinite loop

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 06:05 AM
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();
}
}
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 06:11 AM
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();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 07:16 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2019 01:38 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2019 01:57 PM
Are you trying to add a record to the same table for which the after insert Business rule is written?