Global BR to avoid duplicate records on insert and update

kbanerje
Mega Guru

Hi,

In our instance we are facing issue with duplicate Incident/Change/Problem/sc_task number being created.

We cannot make dictionary unique though we have the script with us to do it and then to remove those before setting number field as unique on task table. Neither we need to correct the old duplicate numbers.

Can anyone help me with a BR on (before insert update)to run globally on all tables only during insert and update so that we can restrict the entry of duplicate records in any table.

ctomasigoswami.sudiptaDeveloper Community

Kind Regards,

Kirtiman

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

It is for a specific table.


you can modify it to global as well


Regards
Harish

View solution in original post

3 REPLIES 3

Harish KM
Kilo Patron
Kilo Patron

below code which i used to prevent duplication



function onBefore(current, previous) {



  if (current.operation() == "update") {



  checkDuplicateUpdate();



  } else {



  checkDuplicateInsert();


  }


}






function checkDuplicateInsert()


{



  if(!gs.nil(current.sys_id))


  {



  var dup = new GlideRecord("tablename");


  dup.addQuery("fieldname", current.value);


  dup.addQuery("u_active", 'true');


      dup.query();


  if (dup.next()) {


  gs.addInfoMessage('Already record exists in the table .Check the existing ID ' +routing_id);


  current.setAbortAction(true);


  }


  }


}




function checkDuplicateUpdate()


{



  if(!gs.nil(current.sys_id))


  {



  var dup = new GlideRecord("u_l1_helpdesk_routing");


var dup = new GlideRecord("tablename");


  dup.addQuery("fieldname", current.value);


  dup.addQuery("u_active", 'true');


      dup.addQuery("sys_id" , "!=" , current.sys_id);


  dup.query();


  if (dup.next()) {


  //gs.addInfoMessage('count' +dup.getRowCount());


  gs.addInfoMessage('Already record exists in the table .Check the existing ID '+drouting_id);


  current.setAbortAction(true);


  }


  }


}


Regards
Harish

kbanerje
Mega Guru

Was that a global BR or running on a specific table?


Harish KM
Kilo Patron
Kilo Patron

It is for a specific table.


you can modify it to global as well


Regards
Harish