- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 02:48 AM
I am trying to avoid a duplicate entry in a table using a business rule but it is not working.
Instead of firing for duplicate entires.. Its fires for everything. even not allowing me to add the fresh entry itself.
I am trying to eliminate a duplicate entry for a serial no. in a computer table.
Here is my code:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number', current.serial_number);
gr.query();
if(gr.next()){
gs.addInfoMessage('There is duplicate value for this serial number in the table');
current.setAbortAction(true);
}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Enterprise Asset Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:16 AM
Hi Raju,
You can have a before BR with the below script.
(function executeRule(current, previous /*null when async*/) {
var grAst = new GlideRecord('cmdb_ci_computer');
grAst.addQuery('serial_number', current.serial_number);
grAst.query();
if (grAst.next() && grAst.sys_id != current.sys_id) {
gs.addErrorMessage("Serial number already exists");
current.setAbortAction(true);
}
})(current, previous);
Hope this helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 05:46 AM
Raju Sardar wrote:
Yes you are right Dave, but as there are already duplicate entries.. I was just afraid that I may loose some data if I make it unique value now. What's the best practice?
I believe uniqueness is checked upon insert/update, but not on existing data, so you should be able to enable it without it harming current records.
However, I'd recommend you clone your instance to a testbed and try it out first. It's always the way with major changes of this kind: don't risk production systems if you're unsure.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2019 06:11 AM
Hi. I am a beginner in service now. Can you please tell me how can I make a field unique in order for me to prevent duplicates?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2019 12:49 AM
Hi Pearl,
There is no OOB logic for this.
You have to write the above mentioned BR(Business Rule) in order to make any field unique and avoid duplicate enteries.
(function executeRule(current, previous /*null when async*/) {
var grAst = new GlideRecord('cmdb_ci_computer');
grAst.addQuery('serial_number', current.serial_number); // you can put your unique field here to make it unique and it doesnt allow to put duplicate entry and will stop user doing that
grAst.query();
if (grAst.next() && grAst.sys_id != current.sys_id) {
gs.addErrorMessage("Serial number already exists");
current.setAbortAction(true);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 04:44 AM
MAke sure that you are running on before insert only.
ake sure you add few gslog statement in it.
Regards,
Shamma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 04:53 AM
All your suggetions were helpful.. A bit of modification made it work. The script was correct which you guys gave. Thank you for your help