Onbefore script transfrom map

Mark Wood
Tera Contributor

Hello Experts,

I'm currently delving into the sources.row method within a transform map script. I have an Excel sheet containing ten records, and I'm looking to import only two specific records: those corresponding to rows 1 and 5. However, despite writing the following script, it doesn't seem to be functioning correctly. Could someone please guide me on this?

Thank you.

Onbefore Script.

 

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

	if(source.row==1 ||source.row==5)
	{

gs.log("do nothing");
	}
	else{
gs.log("Ignore records"+source.row);
	ignore=true;
	}

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

 

 

 

1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hey @Mark Wood,

 

Can you try the following instead:

source.getValue('sys_import_row') == 1 || source.getValue('sys_import_row') == 5

 

And FYI, I don't think using the row to ignore the data is recommended. Instead, you should define a logic by using the actual value of the imported data.

 

Cheers

View solution in original post

4 REPLIES 4

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Mark Wood 

The `onBefore` script in a ServiceNow transform map is used to manipulate data before it is transformed into the target table. If you want to import only the records from rows 1 and 5, you should set `ignore` to `true` for all other rows except for rows 1 and 5. However, the `source.row` property does not exist in ServiceNow's data source API. Instead, you should use `source.getRow()` to get the current row number.

Here's how you can modify your script to correctly ignore all rows except for 1 and 5:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    var currentRow = source.getRow(); // Get the current row number

    if (currentRow == 1 || currentRow == 5) {
        // If it's row 1 or 5, do not ignore (import the record)
        gs.log("Importing record from row: " + currentRow);
    } else {
        // If it's any other row, ignore (do not import the record)
        gs.log("Ignoring record from row: " + currentRow);
        ignore = true; // Set ignore to true to skip the record
    }
})(source, map, log, target);


Make sure to place this script in the `onBefore` script field of your transform map. This script will log a message for each row that is processed, indicating whether the row is being imported or ignored. Only rows 1 and 5 will be imported, and all other rows will be ignored.

Please mark this response as correct or helpful if it assisted you with your question.

Hello @Iraj Shaikh 

Thank you for your assistance.

I attempted to use the script you provided in the OnBefore script event, but it doesn't seem to be functioning correctly. In the system log, I noticed that the current row is showing as undefined. I have attached a screenshot for your reference.

Thank You.

James Chun
Kilo Patron

Hey @Mark Wood,

 

Can you try the following instead:

source.getValue('sys_import_row') == 1 || source.getValue('sys_import_row') == 5

 

And FYI, I don't think using the row to ignore the data is recommended. Instead, you should define a logic by using the actual value of the imported data.

 

Cheers

Thank You @James Chun 

Its working.