- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2021 07:38 PM
Hi All,
I want to prevent duplicate asset tags on assets. I wrote a before insert business rule to check for existing asset tags and compare it with current tag, if both of them matches it will throw a error message. When I use the same code for update, it is not allowing to update any records by throwing error.
How can we prevent duplicate asset tags on update.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2021 09:25 PM
Hi, try excluding the current record, so you only get a result if there is another record.
// Add your code here
var tag = new GlideRecord('alm_asset');
tag.addQuery('asset_tag',current.asset_tag);
tag.addQuery('sys_id', '!=', current.sys_id);
tag.query();
if (tag.next()) {
gs.addErrorMessage("Tag exists");
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2021 10:52 PM
I would expect it to be ok with either, as the sys_id of a record before insert is normally -1,
you could always enclose the sys_id query in an if condition
if(current.operation() == 'update') {
tag.addQuery('sys_id', '!=', current.sys_id);
}
https://docs.servicenow.com/bundle/newyork-application-development/page/script/business-rules/concept/c_DbActionsToTriggerTheBusRule.html