Question about scripting in a Scheduled Job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 11:04 AM
Hello everyone,
I am trying to fill in two fields on the sys_user_groups table based on the sys_user_group.manager reference field. There is a flag on the User table and we need to bring the user with the flag and their manager over to two fields on the group table. I believe that my issue is in the 'return results' (May be wrong) of the below scripts but I don't know how to retrieve the two pieces of data and update the 2 fields on the group table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 11:20 AM - edited 12-11-2023 11:21 AM
Try the following script. Change the field names as per their original names in your configuration.
var qry = 'active=true^managerISNOTEMPTY';
gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery(qry);
gr.query();
while (gr.next()) {
var user = gr.getValue('manager');
var obj = xltUser(user);
gr.setValue('field1', obj.x);
gr.setValue('field2', obj.y);
gr.update();
}
function xltUser(user) {
var myObj = {
x : '',
y : ''
};
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', user);
grUser.query();
if (grUser.next()) {
if (grUser.getValue('flag') == true || grUser.getValue('flag') == '1') {
myObj.x = grUser.getUniqueValue();
myObj.y = grUser.getValue('manager');
return myObj;
} else {
xltUser(grUser.getValue('manager'));
}
}
return myObj;
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 12:13 PM
Hi @David Livingst1 ,
May i know the reason for using scheduled job here.
For me, it seems pretty much a background script or fix script.
var gr =new GlideRecord('sys_user_group');
gr.addEncodedQuery('active=true^managerISNOTEMPTY');
gr.query();
while(gr.next()){
var grUser = new GlideRecord('sys_user');
grUser.get('sys_id',gr.manager);
if(grUser.u_flag == true){
gr.u_field1 = grUser;
gr.u_field2 = grUser.manager;
gr.update();
}
}
Something like this should update your valid group records. Hope this helps.
Regards
Saranesh