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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Jyoti Ranjan Se ,

 

Why are you writing an onBefore script. U can simply use field map for this n make it as coalesce true.

 

Thanks,

Danish

 

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. 

if i want to validate 5 reference filed two have them are not present in source data then how can i write one script for all of them??

 

please help me.

Thanks

jyoti

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