Fix script to add 3 user in a field

Deo Christian V
Tera Contributor

DeoChristianV_0-1745542670209.png

Need a fix script to add 3 users in Co-managers field in a group

11 REPLIES 11

var gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery('nameINCFM.CS.US.Bank-CM,CFM.CS.US.CGS,CFM.CS.US.CMT,CFM.CS.US.CNR,CFM.CS.US.ComsMedia,CFM.CS.US.Energy,CFM.CS.US.FS,CFM.CS.US.Health,CFM.CS.US.HighTech,CFM.CS.US.HPS,CFM.CS.US.Industrial,CFM.CS.US.Insurance,CFM.CS.US.LFS,CFM.CS.US.PRD,CFM.CS.US.PublicSvcs,CFM.CS.US.RES,CFM.CS.US.SoftPlat,CFM.CS.US.Utilities,CGFBA_Pre-contract');
gr.setLimit(19);
gr.query();

if(gr.next()){
    gr.manager = '92882e560f424a0035fde478b1050e44';
    gr.u_co_managers = 'ccd323ce1b3899540cf8edf3604bcbaa,5ffea29e0f424a0035fde478b1050ef0,342d08871392d200daed36666144b06a';
    gr.update();
}
Nothing is being updated here
 

@Deo Christian V 

field "u_co_managers" is list type referring to sys_user table?

then it should work fine

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

DeoChristianV_0-1745821859487.png

DeoChristianV_1-1745821930664.png

 

Image for reference of Co-managers field

@Deo Christian V 

you can do this without scripting as per approach shared by @Robert H 

Your script is correct. you can add logs and debug it further. Hope the sysIds are for active users only

Your script will update only 1 record as you are using IF

Update it with while

var gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery('nameINCFM.CS.US.Bank-CM,CFM.CS.US.CGS,CFM.CS.US.CMT,CFM.CS.US.CNR,CFM.CS.US.ComsMedia,CFM.CS.US.Energy,CFM.CS.US.FS,CFM.CS.US.Health,CFM.CS.US.HighTech,CFM.CS.US.HPS,CFM.CS.US.Industrial,CFM.CS.US.Insurance,CFM.CS.US.LFS,CFM.CS.US.PRD,CFM.CS.US.PublicSvcs,CFM.CS.US.RES,CFM.CS.US.SoftPlat,CFM.CS.US.Utilities,CGFBA_Pre-contract');
gr.setLimit(19);
gr.query();

while(gr.next()){
    gr.manager = '92882e560f424a0035fde478b1050e44';
    gr.u_co_managers = 'ccd323ce1b3899540cf8edf3604bcbaa,5ffea29e0f424a0035fde478b1050ef0,342d08871392d200daed36666144b06a';
    gr.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @Deo Christian V ,

 

Please use a Data Management Job to get this done in 5 minutes without scripting.

 

Regards,

Robert