
- 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:59 PM
I think there is a misunderstanding of my question. I have edited the question to make it more clear. Please check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 11:01 PM
Don't understand your updated question. Can you create a manual example of the auto number you are after?
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 11:07 PM
This question is due to a misunderstanding of the concept.
The sys_number only relates the Number field on the Task table.
But here I am trying to achieve this:
To rephrase the question again:
Forget sys_number table.
How to get User ID field being auto-numbered with a prefix BLM followed by auto-generated numbers? (Only when Department is BLM Market)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2020 11:11 PM
Well you should store somewhere what the latest number is. Of you should do a count on all records matching that department I guess and than +1?
Would be achievable also with Business Rule or Flow.
When should this run? Only on insert? Or already when you manually open a new user form, so on display?
Or well... display won't work I guess, because you do have to select the department. So only on insert?
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 11:18 PM
Only on Insert. While a new user is created, validate if Department for that user is "BLM Market" and if yes - auto-number as BLM+<auto-generated-number>.
NOTE - There is a record producer which populates Department as "BLM Market" on user records generated from a Catalog item. For those records, I want this auto-numbering to happen.