- 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 02:50 AM
add this in
gr.addQuery('sys_id','!=',current.sys_id);
this stops the record from seeing itself...
make sure it's a before BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:06 AM
Thanks mguy for replying, but its not allowing me to enter the fresh record itself... Is there something wrong in my code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:08 AM
HI Raju,
the business rule should be before insert business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:15 AM