
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 06:29 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2020 10:55 PM
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);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 06:56 PM
So, your first step is to set ALL user records to inactive, and then only if they are in your import, set the to active? It's an unusual requirement but; you can gain some efficiency using updateMultiple() instead of a while loop.
Be sure to try this out in a Dev instance! You might also want to...exclude your admin users. Just a thought.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var users = new GlideRecord('sys_user');
users.query();
//Note, always use setValue with updateMultiple!!!
users.setValue('active', false);
users.updateMultiple();
})(source, map, log, target);
If this was helpful or correct, please be kind and click appropriately!
Michael Jones - Proud member of the CloudPires Team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 10:19 PM
Thanks for the help, so this will work? (We actually have inactive users OK to login we disabled the business rule...) I am testing in DEV, but it will probably be tomorrow until I can see if it works or not.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (!gs.hasRole("admin")){
var users = new GlideRecord('sys_user');
users.query();
//Note, always use setValue with updateMultiple!!!
users.setValue('active', false);
users.updateMultiple();
}
})(source, map, log, target);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 10:40 PM
Hi,
corrections to your script
you are using incorrect field name for active at both the places
Test it for some records by adding setLimit() then remove this
onStart
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var users = new GlideRecord('sys_user');
users.setLimit(5); // for testing purpose
users.query();
while(users.next()){
users.active = false;
users.update();
}
})(source, map, log, target);
onAfter;
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
target.active = true;
})(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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 11:29 PM
Yeah but I am using updatemultiple instead of the while let me see if this works, then I'll try yours.