Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Background script for updating multiple records in user table.

Shyna1
Tera Contributor

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

5 REPLIES 5

Anil Lande
Kilo Patron

Hi,

can you please share what you have done by yourself?

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

    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();
}
}

 

 

 

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

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande