Prevent creation of duplicate records

Souvick6917
Tera Contributor

Hello Team,

 

I have created one business rule to create records on a new table based on insert/update on the existing table. The current issue is duplicate records are being created, everytime an insert or update action performed on the existing table. Only one entry of the records are required on the new table.

I want the validation code on the same business rule which is used to create the new records to prevent creation of duplicate records.

It is a before type business rule running on insert/update.Below is the business rule:

(function executeRule(current, previous /*null when async*/ ) {
//Get the comma-separated values from the field
var values = current.user.getDisplayValue();

//Split the values into an array
var valueArray = values.split(',');

//Iterate over each value
valueArray.forEach(function(value){
    //Trim whitespace
    value = value.trim();

    //Create a new record in the x_bnp18_spb_mana_0_pps_user_details table
    var segregatedValue = new GlideRecord('ps_user_details');
    segregatedValue.initialize();
    segregatedValue.users = value;
    segregatedValue.perimeter_number = current.sys_id; //Link back to the original record
    segregatedValue.insert();
});
   

})(current, previous);
2 REPLIES 2

Mark Manders
Mega Patron

You need to first validate if the record already exists or not.

(I copied your code after the validation, so if there are issues in there, those also need to be fixed).

 

var sysID = current.getUniqueValue();

var record = new GlideRecord('ps_user_details');

record.addQuery('perimeter_number', sysID);

record.query();

if(!record){

//Get the comma-separated values from the field
var values = current.user.getDisplayValue();

 

//Split the values into an array
var valueArray = values.split(',');

 

//Iterate over each value
valueArray.forEach(function(value){
    //Trim whitespace
    value = value.trim();

 

    //Create a new record in the x_bnp18_spb_mana_0_pps_user_details table
    var segregatedValue = new GlideRecord('ps_user_details');
    segregatedValue.initialize();
    segregatedValue.users = value;
    segregatedValue.perimeter_number = current.sys_id; //Link back to the original record
    segregatedValue.insert();
}
});

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark,  

I run the above code but it is not working, though it is not throwing any error, but it is not pushing any new record as well on the new table. Don't know why the validation is not working, without it is working everytime but creating multiple duplicate records.

 

Regards

Souvick