- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2023 10:36 PM
Hi Everyone,
we are having a issue with the duplicate asset tags, and already there is a before business rule so that whenever we are trying to create a duplicate record by giving the duplicate asset tag it is restricting to do that. but few of the users are creating the records by giving the space before or after the asset tag so that it is not considering as duplicates and the duplicates have been created successfully. Can someone help me with that.
This is the Business rule we have :
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2023 06:24 AM
To prevent the creation of duplicate asset tags when users add spaces before or after the tag, you can modify the existing business rule to remove any leading or trailing spaces from the `asset_tag` field before querying for duplicates. Here's an example of how to do this:
```
(function executeRule(current, previous /*null when async*/) {
var ciname = current.asset_tag.trim(); // Remove leading/trailing spaces
var gr = new GlideRecord('alm_asset');
gr.addQuery('asset_tag', ciname);
gr.query();
while (gr.next()) {
gs.addErrorMessage('Asset Tag is already in use');
current.setAbortAction(true);
return; // Exit loop early since we found a match
}
})(current, previous);
```
This code uses the `trim()` method to remove any leading or trailing spaces from the `asset_tag` field before querying for duplicates. If a matching record is found, it sets an error message and aborts the creation of the new record.
Note that if you have existing records with leading/trailing spaces in their `asset_tag` field, this code will not identify them as duplicates since it trims those spaces before querying for matches. You may want to consider running a one-time data cleanup script to remove any leading/trailing spaces from existing records.
Thanks
Amarjeet Pal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2023 06:24 AM
To prevent the creation of duplicate asset tags when users add spaces before or after the tag, you can modify the existing business rule to remove any leading or trailing spaces from the `asset_tag` field before querying for duplicates. Here's an example of how to do this:
```
(function executeRule(current, previous /*null when async*/) {
var ciname = current.asset_tag.trim(); // Remove leading/trailing spaces
var gr = new GlideRecord('alm_asset');
gr.addQuery('asset_tag', ciname);
gr.query();
while (gr.next()) {
gs.addErrorMessage('Asset Tag is already in use');
current.setAbortAction(true);
return; // Exit loop early since we found a match
}
})(current, previous);
```
This code uses the `trim()` method to remove any leading or trailing spaces from the `asset_tag` field before querying for duplicates. If a matching record is found, it sets an error message and aborts the creation of the new record.
Note that if you have existing records with leading/trailing spaces in their `asset_tag` field, this code will not identify them as duplicates since it trims those spaces before querying for matches. You may want to consider running a one-time data cleanup script to remove any leading/trailing spaces from existing records.
Thanks
Amarjeet Pal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2023 02:03 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2023 09:43 AM
Hi Amarjeet, Can you help me with the data cleanup script to remove the existing Duplicates.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 11:05 PM - edited ‎11-07-2023 09:06 PM
.