
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 10:38 PM
If I add a new prefix - BLM on the sys_number table.
Now help me write a Business rule that this prefix will only auto-number user_name field on the User table if Department is BLM.
How to achieve this?
Solved! Go to Solution.
- Labels:
-
Multiple Versions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2020 01:41 AM
Maybe a small alternative, which would prevent issues when you are deleting users or removing the department of users: using an actual last number record.
- Just create a record in the sys_number_counter table (don't select a table, just create a record with only a number matching or higher then your last current number)
- Business Rule condition stays on department = your department OR department changes to your department, on insert + on update
Maybe add, user_name does not contain your prefix? But that's up to you.
- Script like (only update the sys_id with the sys_id of your new number record):
(function executeRule(current, previous /*null when async*/) {
var prefixStr = 'BLM';
var numberInt = 1000000;
var grCounter = new GlideRecord('sys_number_counter');
grCounter.get('be237ed32fb3d890b0c2d5ea2799b62a');
var lastInt = grCounter.getValue('number');
current.user_name = prefixStr + (parseInt(numberInt) + parseInt(lastInt) + 1);
grCounter.addValue('number', 1);
grCounter.update();
})(current, previous);
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 10:43 PM
Hi there,
You can only use 1 prefix on a table. Sure you can technically change the number value for certain records or think of a custom creation to mimic the auto number. Though just having 2 auto numbers with ease: no go.
Also note that changing the prefix manually so it's different than the actual auto number prefix: this will have some implications. For example, you can't search such a custom number on the direct search, it would give you 0 results.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 10:46 PM
The sys_user table does not hold a record in the sys_number table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 10:51 PM
Ah didn't read the sys_user part, my bad. So than you can just add it?
Only thing different reading your question again, just on every insert a new number will be generated. That's just the behavior of auto-number which I wouldn't touch.
You could obviously script to make the number field empty again, Business rule or Flow for example, though you would have gaps in your number range. Number is just a field value, so you could just empty it scripted for the records in your logic.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 10:56 PM
Also just tested with a no code Business Rule. Works instantly:
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field