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.

Transformation script,hHow to get Sys ID of Current Record

Meloper
Kilo Sage

Hi,

i have on big Data Source and create a ImportsetTable and a Transform Map.

The Transform Map works with some File Mappings.

Now i want to write Transform Script to fill another Table with Values of these Data Source.

In my opinion i can't use another Transform Map ,because a field on the new table is a reference field to the record from the field mapping and the records are otherwise unrelated. 
The only relation ist that the Values are in one Import Record.

E.g.

Table A (Field Mapping):

Record 1
Field 1: String
Field 2: String
Field 3: String

Table B (Gliderecord in Transform Script)

Record 1
Field 1: String
Field 2: String
Field 3: Reference to Table A (Record 1)

Maybe;

Var gr= new GlideRecord(Table B);
gr.initialize();
gr.Field_1 = source.example1; 
gr.Field_2 = source.example2; 
gr.Field_3 = target.sys_id? (Sys ID of Table A Record?)
gr.insert();

So how ist it possible to set the Field Value of Field 3 in Table B.

The transform logic runs through each record.
But can I intercept the SYS ID of the actual transformation (source table -> target table) in a transform script?

Or do I have to write a script "After" which then runs through the target table and performs the action for each record?

 

And which Event Name do i have to choose?

onAfter?

targetGlideRecordThe row of the target table that is currently being processed.
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Use onAfter() and target.sys_id- As per the logic you have given- target.sys_id returns the Sys ID of the Table A record and use onAfter() to run the logic after each row transformation into a target record.

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Use onAfter() and target.sys_id- As per the logic you have given- target.sys_id returns the Sys ID of the Table A record and use onAfter() to run the logic after each row transformation into a target record.

Thank you very much, so I practically answered my own question?

Hi,

you can use onAfter transform script for inserting/updating any value in some other target table

This would run on every row being processed

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi

 

I have a similar issue I need to update the work notes based on the sys_id from the inserted or the updated record this is my script:

var value = source.u_work_notes;

                               if(value ==""){

                                               value = "The data has been updated/created through the transform map: "+map.name;

                               }

                               var elID = target.sys_id;

                               gs.info("AZ get SysID Work Notes "+elID);

        var glJour = new GlideRecord("sys_journal_field");

        glJour.addQuery("element_id", elID);

        glJour.addQuery("value", value);

        glJour.query();

        if (glJour.next()) {

            gs.info("Name: " + glJour.element + " Table " + glJour.name + " value " + glJour.value + " sysid " + glJour.sys_id + " element ID " + glJour.element_id);

        } else {

            gs.info("insert new Val to " + elID);

            glJour.element = "u_work_notes";

            glJour.name = map.target_table;

            glJour.value = value;

            glJour.element_id = elID;

            glJour.setWorkflow(false);

            glJour.insert();

        }

I am getting the sys id while I update the record but during a data insert the ID is getting as blank, is there any solution for this?