Transform Script: Populate CI location based on assigned to field

Alex Macdonel
Tera Expert

I'm currently working doing a regular import of CI's that are assigned to different individuals and I'd like the CI location to inherit its location from the user at time of import.

I've written a couple of transform map scripts but they are not working (a big and ugly script follows):

var suser = source.u_username;

var tuser = new GlideRecord("sys_user");

var loc = "";

tuser.addQuery("user_name", suser);

tuser.query();

if (tuser.next) {

  loc = tuser.location.getDisplayValue();

}

target.location = loc;

And with this script I get the following error:

org.mozilla.javascript.EcmaError: "target" is not defined.

Caused by error in <refname> at line 10

7: if (tuser.next) {

8: loc = tuser.location.sys_id;

9: }

==> 10: target.location = loc;

What is the best way to do this at the time I do the import, is a transform script the way to go or would it be better to do an after import script?

Thanks,

Alex

1 ACCEPTED SOLUTION

Hi Alejandro,



Where are you using the script?


If its in a Field Map Script, you should end with :


answer = loc;



If its in an onAfter or onBefore script, then target should work fine and it would implicate your fieldname 'location' is wrong


View solution in original post

4 REPLIES 4

Brian Dailey1
Kilo Sage

Hi Alejandro,



You should be able to set values on target in your Transform script.   However I do see a couple of issues...



  1. There is some inconsistency between your code and what's in the error message... but 'target.location' is a reference, so you would need to set it in either of these two ways:
    target.setDisplayValue('location', loc);//assuming loc contains the display value and not the sys_id
    or
    target.location = tuser.location.sys_id;
  2. When using 'tuser.next', you need to include the parentheses since this is a method:
    i.e.,         if(tuser.next()){
                                ...


Maybe see if either of those tips makes any difference.




Thanks,


-Brian


Hi Alejandro,



Where are you using the script?


If its in a Field Map Script, you should end with :


answer = loc;



If its in an onAfter or onBefore script, then target should work fine and it would implicate your fieldname 'location' is wrong


Yay! changing the final line did the trick:



        answer = loc;



It's quite bad that the wiki article that talks about transform map scripts doesn't make reference to this behavior. And to make it annoying, I recently made a script that is a one liner and in it I was able to use the target variable to set a value.



This is the script I'll implement as it's much smaller and pleasant to the eye:



        var gr = new GlideRecord("sys_user");


        if(gr.get("user_name",source.u_username)) {


          answer = gr.location;


        }



Thanks for the help!


Yeah, the wiki could be more consistent I agree...


Though somewhere else in the wiki it gives the correct answer


Glad I was able to help though