Don't want duplicate values in table - Need to know best practice

geet
Tera Guru

Hi All,

I have a table as follows:

find_real_file.png

I don't want duplicate values as follows:

find_real_file.png

I want to put a validation that user should not be able to submit the record if there is already a record with 2500 limit.

I want to know whether it will be a best practice to put validation as if there are 10,000 records with 2500 limit value, will it be going to impact my system performance.

Regards,

Shikha Khanna

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shikha,



Are you saying you don't need duplicate values in the Limit column when the record is created by user?


If yes then have before insert business rule and query the same table with the value of limit field and if found use current.setAbortAction(true);



Script:



var gr = new GlideRecord('table_name');


gr.addQuery('limit',current.u_limit);


gr.query();


if(gr.next()){


gs.addInfoMessage('There is duplicate value for limit in the table');


current.setAbortAction(true);


}



Mark Correct if this solves your issue and also hit Like and 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

11 REPLIES 11

vinothkumar
Tera Guru

Hi Shikha,



You can make the field value unique by clicking on the unique check box on the dictionarr entry of that record, however it will result in data loss, since you have mentioned already duplicate values are there.



Try to create Before Insert business rule, to check the field and give info to the user that there is already a record with same value.



Creating New Fields - ServiceNow Wiki



Below BR is similar for making the duplicate number prevention, however you can use the same just by modifying the field name with number field


var curNum = current.limit+ '';



if(curNum) {



  var recordClass = current.getRecordClassName();


  var gr = new GlideRecord(recordClass);


  gr.addQuery('limit', curNum);


  gr.query();



  if(gr.getRowCount() > 0) {


   


    gs.addInfoMessage("Limit was already available, please select a different limit.);


    current.setAbortAction(true);


  }


}


Venkateswarlu K
Mega Guru

HI Shikha



can u pls try this in business rule


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




  var Matching = new GlideRecord('table');


  if(Matching.get('subsidary_id', current.subsidary_id)) {


  gs.addErrorMessage(gs.getMessage("same record already exists."));


  current.setAbortAction(true);


  }


})(current, previous);


I hope this will helpful to u