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

No. I was just explaining what unique things I'm doing with the transform map since you asked what else it was doing. 

Some of these users are managers, but that wouldn't affect this since we are looking at the user_name field and not the manager field. 

I initially thought this would be something easily configured through a business rule, but I was unable to get that to work either. Part of the reason was those other business rules I had mentioned though which were overriding updates. 

Any other thoughts or tests to try?

Thanks for the updates, what i was thinking since all these uses are also there in source csv file. can we try to run the same code with onComplete(), once the transformation is done for the records then let inactive the users which we want explicitly. 

Ok...changing the Transform Script to onComplete worked. The users specified in the script were set to inactive and all other users were unaffected. 

Prior to getting this reply I had updated the script we were using in the transform map (not under the Transform Map tab) to the following and it also worked.

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

if(source.u_login == 'nhesgz'||source.u_login == 'teccmm')
target.active = false;

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

 

So now I have it successfully running by two different methods. You have been extremely helpful. Could you explain the difference between the two and any benefit/s per each method?

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.

Ok, I have the following working without issue. 

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

if(source.u_login == 'nhesgz'||source.u_login == 'teccmm'||source.u_login == 'jpfdjg'||source.u_login == 'CCSRLB'||source.u_login == 'tecrlc'||source.u_login == 'mhhsgx'||source.u_login == 'nrslkz'||source.u_login == 'teckll'||source.u_login == 'teckxb'||source.u_login == 'tecldl'||source.u_login == 'tecdlc')

target.active = true;

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

 

However, I need to have the script update 2 fields. I've tried two different ways. 

First (this activated all the users in the import rather than limiting to the above list in the script)

target.active = true;

target.locked_out = false;

Second (this had errors in the script editor so I couldn't save the sript. Bad assignment, Missing semicolon, Expected an assignment or function or function call and instead saw an expression.)

target.locked_out = false && target.active = true;