- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2016 12:56 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2016 01:16 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2016 01:15 PM
Hi Alejandro,
You should be able to set values on target in your Transform script. However I do see a couple of issues...
- 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; - 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2016 01:16 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2016 09:34 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 12:58 AM
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