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

For your field mapping (one for each target field), check the "Use source script" field and just return the value you want.  It will populate your "Target field" with that value.

 

find_real_file.png

MGanon
Tera Guru

"CAT", "DOG", & "BIRD" are variables, not constants. The values of these variables might be different for each record to be imported. For example, each source record might include source.breed, each with a different of breed of cat. The transform map might need to use source.breed to query a separate table to retrieve where that cat is most popular. The DOG variable might be populated by a similar query and BIRD might use the values of CAT and DOG to calculate where birds are safest. The example is childish but the point is to run the queries once for each record, not once for each field within each record.

Tim Provin
Mega Guru

Well if you are using the source script, you can do whatever you want with it.  To access the breed column in the source record you would just use source.u_breed.  Then you can do a GlideRecord query against your table and return whatever the result is.

MGanon
Tera Guru

Hi Tim,
I think that I see where you were going. Instead of setting variables to use later in the transform map for fields or onBefore scripts on the "target" record to be imported, I think that you are recommending that I set the values directly in the transform map script. Thank you!

To take the next step, I want the transform map to call a Script Includes so I can reuse the code. That does not work. For my example, I want to update the sys_user table with the result of the ANIMAL query. (Understanding that it makes no sense to update a user's middle name or phone with an animal value, the example is fake.)
Transform Map script:
 //attempt to update the middle name of the imported target going into the sys_user table
 IMPORT.ANIMAL(source, target); //The script includes attempts to update the target's middle_name
 
 //I also tried to directly update the target.phone by calling the same IMPORT Script Include:
 (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 = IMPORT.ANIMAL(source, target)
 })(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){
 //run script that appears to work to return values
 //using "source" as the input values
 var CAT = ... //populating the variable to in the Script Includes should to be functioning because it works in other scripts
 message += '/nMessage expected to write to Import log but doesn't';
 gs.log = ("Another message expected to write to Import log but doesn't");
 target.middle_name = CAT;
 //I cannot get the returned value out of the script
 return;
}

 

I attempted to post earlier so apologies if duplicated.