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

Hi Ankur,



I have implemented your solution as follows, but i need to understand 1 thing.



(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('x_mkgaa_credit_car_credit_card_limit_calculation');
  gr.addQuery('u_subsiary_id', current.u_subsiary_id);
  gr.addQuery('u_currency', current.u_currency);
  gr.addQuery('u_limit', current.u_limit);
  gr.query();
  if(gr.next())
  {
  gs.addErrorMessage(current.u_limit+' Limit already exist for this subsidiary and currency. Please put a different limit value');
  current.setAbortAction(true);
  current.u_limit ='';
  }
})(current, previous);


Que: Right now i have only 12 records and gliding only 12 records will not affect my instance performance. What if i have 100000 records and i am gliding limit for all these records. Would   it affect my instance performance?



Regards,


Shikha Khanna


shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Hi Shikha,



See if Unique index on fields will help you. From the comments above it seems like you want combination of two fields unique (subsidiary ID and currency). This can be guaranteed by unique index on subsidiary ID and currency. Means combination of values will be unique.



http://wiki.servicenow.com/index.php?title=Unique_Index#gsc.tab=0


Hi Shikha,



Any update on this? Can you mark my answer as correct, helpful and hit like if you were able to achieve your requirement. This helps in removing this question from unanswered list and helps others to search similar question quicker. Thanks in advance.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Done: Thanx for always helping 🙂


I tried this and even though I don't have a duplicate and it does not create the record, I still get an error.

    findDupeCircuitID();
    function findDupeCircuitID() {
        var gr = new GlideRecord("u_network_circuit");
        gr.addQuery("u_ciruit_id", current.u_ciruit_id);
        gr.query();
        if (gr.hasNext()) {
            gs.addErrorMessage("Circuit ID already exists");
            current.setAbortAction(true);
        }
    }
   

It's on a Before Business rule.

 

Any thoughts?

 

Lisa