Onbefore script to remove duplicates from import table

jackinsidend
Giga Guru

I have an jdbc connection that pulls in about 8000 records, I want to process all that are not duplicates.

I have a serial number field, I would like to not even write the duplicated to my import set table. Is this possible in an onbefore script?

Thanks,

Jack

6 REPLIES 6

Ecelik3
Tera Expert

Hello,

 

For your import set table write a business rule that prevent duplicates such:

 

When = before 

insert = true

 

var existing = new GlideRecord("Your_Import_Set_table_name"); 
existing.addQuery("serial_number", current.serial_number); //Change with your serial number field name
existing.setLimit(1);
existing.query();

if (existing.getRowCount() > 0) {
gs.log("Aborting insertion of serial number:  " + current.serial_number + "  + " already exists");
current.setAbortAction(true);
return;
}

 

 

Thanks

jackinsidend
Giga Guru

Thank you for the reply. So I tweaked the code but it just seems to INSERT and ignore it, even though I set it as a before insert rule:

 

(function executeRule(current, previous /*null when async*/) {

var existing = new GlideRecord("u_sccm_pc_by_build_collections");
existing.addQuery("u_biosserialnumber", current.u_biosserialnumber); //Change with your serial number field name
existing.setLimit(1);
existing.query();


if (existing.getRowCount() > 0) {
gs.log("Aborting insertion of serial number: " + current.u_biosserialnumber + 'already exists');
current.setAbortAction(true);
return;
}
}
)(current, previous);

 

Thanks

jackinsidend
Giga Guru

I also should say, I would prefer neither the original or the duplicate be kept in my import table, I would want to hand those elsewhere.

Jack

ARG645
Tera Guru
I would suggest not to put a Business Rule on the import set table. In complexity stand point think of it of the below way: You will be teiggering the Business rule 8000 times in your scenrio. On top of that, Everytime the BR triggers, it glideQueries for Duplicates. This will slow down the import process itself. What I would suggest is: Do not restrict the imports. Put logic in onStart script such that it queries for duplictes and removes them (Use GlideAggregate)