The CreatorCon Call for Content is officially open! Get started here.

Issue with "onAfter" script with in transform map.

kamal11
Giga Expert

Hi All,

We are facing an issue with the onafter script that we use in transform.

we are trying to bulk update the Records and we want the records which have been updated to update "work-notes" as well.

We have used the following code

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

if(action == "update")

target.u_work_notes = "The Ordered fields of the asset have been updated as per STRY##";

gs.log('The asset with the following serial number is updated: ' + target.serial_number);

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

How ever

it isnt updating the record worknotes

Please let us know if we have to correct something on the same.

Thank you,

Kamal

22 REPLIES 22

Oh sorry, it's also not current.state



gr.addQuery('sys_import_state', 'updated');


mbourla
Giga Guru

To echo what other people have said, by the time the onAfter script runs, the record has already been saved to the database.  So it's too late to modify the "target" record.  

If you want to set the Work Notes field (whether it's work_notes or u_work_notes), that needs doing before the record is updated in the database.  Options:

  1. Do it instead in an onBefore transform map script

  2. Have a field map script for the Work Notes field, and put the code in that field map script

  3. Or do in the main transform map script, that you see if you check the "Run script" check box on the transform map.

Any of those would work, and in any of them the transform will know whether it's an update or an insert, so the check on action == "update" will work.  I'd say the best/correct place would be #2.  

The nice thing about #2 is that the individual field map scripts are basically designed so that you just return the value you want set in the target.  It's clean.  (Something weird is that you don't get the "syntax highlighting" in the Source Script field; I have no idea why that would be; so it feels like you are writing script in Notepad.)

One issue I had with option #1 is that if you modify the Source fields, are are actually permanently modifying the data in your Staging table.  This could be confusing to someone looking back at that data, or could trip you up if you "Reprocess" the records (set them to Pending and do multiple Transforms) because you will not be starting from the same point as when you first loaded the records.