Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Assign Variables to Transform Map

MGanon
Tera Guru

I have a script that I want to write in a Script Includes called from a Transform Map. I also want to use results from the script in several fields. The results of the script will change for each imported record. I assume that I need to initialize the Script Includes from an onStart transform script. To avoid running the same query for each field, can I call the Script Includes from an onBefore transform script to populate variables that the different fields can reuse? How?

Here is an example scenario:
Transform map: IMPORT
Script Includes: ANIMAL
Global variables for each record: CAT, DOG, BIRD

Field1 needs the value of CAT
Field2 needs the value of DOG
Field3 runs another script X using input variables CAT and DOG
Field4 needs the value of BIRD

How do I populate variables CAT, DOG, & BIRD once for each record so I don't have to run the query separately in each field (3 times for each record)?

1 ACCEPTED SOLUTION

Tim Provin
Mega Guru

You shouldn't have any issue with using a script include.  In your field level mapping script make sure you call the script include like the following.

(function transformEntry(source, target, map, log, isUpdate){
  //Call the IMPORT script includes ANIMAL function to update the "phone" field of the sys_user table
  target.phone = new IMPORT().ANIMAL(source, target)
 })(source, target, map, log, action==="update");

Then in your function within the script include make sure that you can returning the value that you want to be set into the field.

View solution in original post

6 REPLIES 6

Tim Provin
Mega Guru

You shouldn't have any issue with using a script include.  In your field level mapping script make sure you call the script include like the following.

(function transformEntry(source, target, map, log, isUpdate){
  //Call the IMPORT script includes ANIMAL function to update the "phone" field of the sys_user table
  target.phone = new IMPORT().ANIMAL(source, target)
 })(source, target, map, log, action==="update");

Then in your function within the script include make sure that you can returning the value that you want to be set into the field.

MGanon
Tera Guru

I pulled the script includes out of the sample based on the ldaputils and created a separate script includes. That may or may not have helped. The main issue was the "message += ...' line.

After discovering the power, the script include called from a transform map will update multiple fields in the target table directly AND/or will return a single value back to the transform map script to update the target there instead.


Transform Map script:
 target.field1 = IMPORT.ANIMAL(source, target); //1 option that updates the target field with the single value returned by the script includes.
 (function transformEntry(source, target, map, log, isUpdate){
  target.field2 = IMPORT.ANIMAL(source, target) //updates the selected field of the target record; script call must have some variable or field to work
 })(source, target, map, log, action==="update");


IMPORT Script Includes:
gs.includes("PrototypeServer"); //patterned after ldaputils script includes
var IMPORT = Class.create();
ANIMAL: function(source, target){
 var CAT = ... //populates the variable to in the Script Includes
 var DOG = ... //populates the variable to in the Script Includes
 //**BREAKS the script: message += '/nMessage expected to write to Import log but doesn't';
 //**Still does not write to the import log as I would have expected but doesn't prevent the rest of the script: gs.log = ("Another message expected to write to Import log but doesn't");
 target.field4 = BIRD;
 target.field5 = "ServiceNow ROCKS!";
 return;
}