Prevent duplicate values on a field - on insert and duplicate

pramodkumar
Tera Expert

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!

1 ACCEPTED SOLUTION

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);

}

View solution in original post

5 REPLIES 5

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