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

Tony Chatfield1
Kilo Patron

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

 

Hi @Tony Chatfield 

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

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

}

Hi Tony,

so by excluding current record, a single business rule will work for both insert and update. correct?

 

Thanks!