Background script for updating multiple records in user table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2022 03:08 AM
Hi,
I need a background script that will basically update a particular field in the user record after querying from sys_user_grmember table.
requirement: I need to check in the sys_user_grmember table by querying if group is active, group.company is abc and group is active and user.project is xyz
when i get all the lists from here , i need to update the 'designation' field(this field is present in user record in user table) of these users listed and update to a sys id .
How to write a sanitized background script for this .
Thanks!1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2022 03:24 AM
Hi,
can you please share what you have done by yourself?
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2022 03:31 AM
You can use script like below:
var grMem = new GlideRecord(sys_user_grmember);
grMem.addEncodedQuery('group.active=true^group.u_company=<company_id>^user.u_project=<project_id>');
grMem.query();
while(grMem.next()){
var userGr = new GlideRecord('sys_user');
userGr.get(grMem.user.toString());
userGr.designation = 'your value here';
userGr.update();
}
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2022 03:38 AM
var qry="group.active=true^user.company=d6f548d7823100d4e7u9j56890f89^group.typeLIKE8b9062b71b6d3498guijbf3604bcv78^group.u_company=d6f548d7823100d4e7u9j56890f89"
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(qry);
gr.query();
var updateCount = 0;
while (gr.next()) {
var usr=new GlideRecord('sys_user');
usr.addQuery('sys_id',gr.user.sys_id);
usr.query();
while(usr.next()){
usr.setValue('department','dfr678989778vkjkhfkddtgy80965r6')
usr.setWorkflow(false);
usr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2022 03:44 AM
This looks good to me.
Can you please try it by putting some logs and see whether it is going in each loop or not.
Thanks,
Anil Lande
Thanks
Anil Lande
