Two fields should have unique combination

Mr_Avinash
Kilo Expert

There are two columns A and B in target table and when importing the data from source the values in the A and B should not repeat(should not be duplicate)

im writing below code but it is not working

var bil = source.u_xyz;
var rec =source.u_jkc;

var grAst = new GlideRecord('u_abcdef');

grAst.addQuery('xyz', bil);

grAst.addQuery(jkc', rec);
grAst.query();

if (grAst.next()){  {
errorFlag = true;
log.error(errorMessage, "File rejected due to multiple entries );
current.setAbortAction(true);
}
else{
target.xyz =bil;
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Avinash,

Since combination of those 2 fields is unique; why not have coalesce on those 2 fields if you are using data source, field map and transform map etc

I assume your script is in onBefore transform script; you cannot use current object there; but use ignore=true so it would ignore the entire source row

var bil = source.u_xyz;
var rec =source.u_jkc;

var grAst = new GlideRecord('u_abcdef');

grAst.addQuery('xyz', bil);

grAst.addQuery('jkc', rec);
grAst.query();

if (grAst.next()){  {
errorFlag = true;
log.error(errorMessage, "File rejected due to multiple entries );
ignore = true;
}
else{
target.xyz =bil;

target.jkc = rec; // check whether this line of code is required
}

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Avinash,

Since combination of those 2 fields is unique; why not have coalesce on those 2 fields if you are using data source, field map and transform map etc

I assume your script is in onBefore transform script; you cannot use current object there; but use ignore=true so it would ignore the entire source row

var bil = source.u_xyz;
var rec =source.u_jkc;

var grAst = new GlideRecord('u_abcdef');

grAst.addQuery('xyz', bil);

grAst.addQuery('jkc', rec);
grAst.query();

if (grAst.next()){  {
errorFlag = true;
log.error(errorMessage, "File rejected due to multiple entries );
ignore = true;
}
else{
target.xyz =bil;

target.jkc = rec; // check whether this line of code is required
}

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

asifnoor
Kilo Patron

Hi,

If the combination of A and B columns shall not repeat, then its best to create a coalesce on these 2 columns. That way, it will update existing record and not create new record.

 

Kalaiarasan Pus
Giga Sage

As stated by Asif, if you set both the fields maps as coalesce, you would not need a script.