Transform Script to Change value for specific users

David Casper
Tera Guru

I have a scheduled data import, using a transform map, to import our users no a daily basis. One of the included fields is 'Enabled' (source) to update the 'Active' (target) field in ServiceNow.

The need is to have a script that looks for a handful of users and changes the Enabled or Active value for them, but only them. I'm a beginner when it comes to programming and have found that this could be done with a onBefore script or maybe a source script.

Latest test is with an onBefore which is allowing the transform to run, but the value is not changing. 

 

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

if(source.login == 'nhesgz');
target.active = false;

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

 

Looking for the best way to do this. Any and all suggestions are welcome. 

1 ACCEPTED SOLUTION

okay, so when we run the script in transform map it run for each row and it will check if source.u_login matches and then it will update at run time. 

 

onComplete() will run once when all the transformations are done for all the records (even after for the user which we wanted to set as inactive) and then it will make those users inactive. 

 

I believe using transform map is better option since you will be checking the u_login user at run time for each row transform, and would taking action right away, instead of using onComplete() with GlideRecord query which will try to glide the sys_user table everytime for every specific records.

View solution in original post

34 REPLIES 34

in log you will get the same error since for log you are still using login, 

 

Please try with below statement and see if this helps.

gs.log('++++++++source.login+++++++++++'+source.u_login);

 

 

This time is returned the u_login value which is a 6 character login. However the user record that is specified in the script is still set a true and is active. 

I am able to activate/deactivate without issue wen just using the CSV as long as I ensure the fale/true values for the active field are lower case, which I've done in the script too. 

other thoughts?

Can you please run the script in transform map script instead of onBefore(). 

 

find_real_file.png

That worked great!!! As I mentioned still learning so have to understand where to place the scripts and how that causes them to function differently. 

I'm about to test, but since I need to apply this to multiple users, would the script take the 'IN' operator in order for me to specify all the logins or would I need to use the || operator?

I don't think you need any operators, the transform map will run for each row separatly.

 

You can refer this link to understand more clearly when and how we should the transform script: https://old.wiki/index.php/Transform_Map_Scripts

 

It's a old wiki URL but will give you any idea how it works. (couldn't get the same URL on new docs.service-now.com)