- 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
‎09-04-2019 03:07 PM
Hey, I want to do it without Serial Numbers. If some one enters a CI with the same name, it should not allow the user to submit the CI. Any idea how would that work.
Any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 03:03 AM
this code is working for one field .I want to use for first it will check id and If the id matches then it will check name, address and phone number.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 11:31 PM
Hi
Thank you it's really helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:17 AM
There is no issue in your script, its perfectly fine. There can be some records having empty serial numbers and I think you are trying to enter a record with empty serial number. Please try below code once;
(function executeRule(current, previous /*null when async*/) {
if(current.serial_number == '' || current.serial_number == null)
{
gs.addErrorMessage('Serial Number cannot be empty');
current.setAbortAction(true);
}
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number', current.serial_number);
gr.addNotNullQuery('serial_number');
gr.query();
if(gr.next()){
gs.addErrorMessage('There is duplicate value for this serial number in the table');
current.setAbortAction(true);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 03:36 AM
Hi Swathi,
Thank you for the script provided. Its working but it is not saving the date for the other fields when I create a new record. But it allows to update a record. for example please see I have created a record. which will only save the serial no. but not the name of the computer.
But when I open the record, then it allows me to update the name field which was blank initially. Please find 4 screen shots which I have sent.
Thank you so much for your help.. I appreciate this.
Regards,
Raju Singh