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

I don't follow on not needing operators. My if statement  if(source.u_login == 'nhesgz')  is only specifying one user at the moment. I need to specify multiple users. 

I tried this, f(source.u_login,'IN','nhesgz,teccmm,jpfdjg,CCSRLB,tecrlc,mhhsgx,nrslkz,teckll,teckxb,tecldl,tecdlc') and it actually deactivated move of the users and messed up the user list layout. Very odd. I activated all the users again using the admin account so seem to be good there. Luckily this is DEV 🙂 

Could you provide an example of how I should update the script to accommodate multiple users.

Thanks

 

you can have user_name field set as coalesce in Field Maps and have just this script in transform map and try. I believe this should help.

 

(function transformRow(source, target, map, log, isUpdate) {
target.active = false;
})(source, target, map, log, action==="update");

 

Like this, 

 

find_real_file.png

I do have user_name set as the coalesce field, but I don't want all the names to be set to inactive (false). I want to specify more than the one I did in testing. 

As I mentioned I tried the 'IN' operator but that really messed things up. I have not tried the || (OR) operator yet. 

I think this is what you need.


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


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

// Store the user list in a variable
var myuserlist = 'nhesgz,teccmm,jpfdjg,CCSRLB,tecrlc,mhhsgx,nrslkz,teckll,teckxb,tecldl,tecdlc';

if(myuserlist.indexOf(source.u_login)>-1)

{
target.active = false;

}

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

 


Please mark this response as correct or helpful if it assisted you with your question.