- 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 08:20 PM
Hi, based on your basic description I am guessing you are running a glide query to find a match on the asset tage field? For an update you would probabaly need to exclude the current record from your query, otherwise the current record will always mean the result is 1 for a unique value and 2 for a duplicate (unless you are changing the current.asset_tag?
if you share your code the forum will be able to review and advise,
otherwise we are just guessing at your intent, plain text or xml is best as screen shots do not allow disgnostics/testing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2021 08:29 PM
Hi
Thanks for the reply. Below is my before insert businessrule code
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var tag = new GlideRecord('alm_asset');
tag.addQuery('asset_tag',current.asset_tag);
tag.query();
if (tag.next()) {
gs.addErrorMessage("Tag exists");
current.setAbortAction(true);
}
})(current, previous);
- 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:28 PM
Hi Tony,
so by excluding current record, a single business rule will work for both insert and update. correct?
Thanks!