To ignore record insert when source data not found data in target table reference field.

Jyoti Ranjan Se
Tera Contributor

I wrote a before transform map event script. but it still not working . 

can anyone please help me on this . and please verify what are the  error in this script.Screenshot 2024-01-05 093308.png

2 ACCEPTED SOLUTIONS

Sainath N
Mega Sage
Mega Sage

@Jyoti Ranjan Se : Try with the below script by replacing it with your field name.

 

 

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

    if (action == 'insert' && JSUtil.notNil(source.your_field_name)) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('first_name', source.your_field_name);
        gr.query();
        if (!gr.next()) {
            ignore = true;
        }
    }


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

 

2. This also can be done with field mapping, with choice action.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution. 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Jyoti Ranjan Se 

your Reference variable is not defined and script won't know what it contains

please add this line

var Reference = source.u_fieldName; // give here the field name

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

Tai Vu
Kilo Patron
Kilo Patron

Hi @Jyoti Ranjan Se 

When a Reference field type is not found, if you'd like to ignore creating a new record in the referenced table, you can set the Choice action to one of the following values:

  • ignore – ignores new records in the reference field and completes processing of all other fields in the transform map.
  • reject – stops the transform for the entire record.

Sample.

Timi_0-1704430230539.png

 

 

If you'd like to ignore any new insert in your transform map, you can use the following script:

 

(function transformRow(source, target, map, log, isUpdate) {

    if (action == 'insert') {
        ignore = true;
    }

})(source, target, map, log, action === "update");

 

 

Timi_1-1704430342122.png

 

Cheers,

Tai Vu