Transform Script to show active/inactive users.

Andrew112
Kilo Guru

Hello, I have a transform script on a table transform map that I want to go through all the users, initially set to inactive, and then set to active to make sure that only active users are in the User table everytime the User list is loaded from external source. (It is loaded every day at a set time)

I have the following: 

onStart 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var users = new GlideRecord('sys_user');
users.query();
var user = users.next();
while(user !== null){
	user.Active = false;
	user = users.next();
}
})(source, map, log, target);

onAfter

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

target.Active = true;

})(source, map, log, target);

It has been 1.5 hours, and it is still running. 

Is there any better way to accomplish what I am trying to do here?

1 ACCEPTED SOLUTION

Hi Andrew,

you need to identify the users and the roles

form the query from sys_user table the users you want and roles you need to check

copy that query and use in script as below

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var query = 'nameINAbel Tuter,Suresh Parab^roles=itil'; // place your query here

var users = new GlideRecord('sys_user');
users.setLimit(5); // for testing purpose

users.addEncodedQuery(query);

users.query();
users.setValue('active', false);
users.updateMultiple();


})(source, map, log, target);

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

13 REPLIES 13

Hi Andrew,

yes updateMultiple() is more efficient so you can use that instead of update()

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var users = new GlideRecord('sys_user');
users.setLimit(5); // for testing purpose
users.query();
users.setValue('active', false);
users.updateMultiple();


})(source, map, log, target);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

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

How do I only do this for users only with certain roles?  

Hi Andrew,

you need to identify the users and the roles

form the query from sys_user table the users you want and roles you need to check

copy that query and use in script as below

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var query = 'nameINAbel Tuter,Suresh Parab^roles=itil'; // place your query here

var users = new GlideRecord('sys_user');
users.setLimit(5); // for testing purpose

users.addEncodedQuery(query);

users.query();
users.setValue('active', false);
users.updateMultiple();


})(source, map, log, target);

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Thanks this helped a lot! Thats exactly what I want to do.

Hope you are doing good.

Any update on this thread?

Regards
Ankur

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