Auto-numbering

Anish Reghu
Kilo Sage
Kilo Sage

 

If I add a new prefix - BLM on the sys_number table.

find_real_file.png

 

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?

1 ACCEPTED SOLUTION

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

LinkedIn

View solution in original post

48 REPLIES 48

Made a really quick test. Works instantly for insert. Obivously, this only covers inserts, not when someone is changing department.

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

	var prefixStr = 'BLM';
	var totalInt = 1000001;
	
	var ga = new GlideAggregate(current.getTableName());
	ga.addQuery('department', '8898b18a3b700300d901655593efc4aa');
	ga.addAggregate('COUNT');
	ga._query();

	if(ga._next()) {
		totalInt = totalInt + ga.getAggregate('COUNT');
	}
	
	current.user_name = prefixStr + totalInt;	

})(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

LinkedIn

This works. Thank you, but can we code it to auto-number records of BLM users in a serial manner? I understand since you are appending the COUNT, it is numbering count value to the prefix.

 

I would prefer it to be this way:

Glide Record into the sys_user.

Query if it is department - X

If yes, save the record with auto-numbered user id.

While auto-numbering validate if the auto-number generated above is unique in the sys_user table.

 

Sorry for so much of trouble.

Obviously it depends on your way of working what error handling you need to build in, etc.. Or you might even need to store the number in a different table, like the auto number and then always just do +1 or something. Though it's really up to you.

This is just a basic example of what could work.

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

LinkedIn

It works, I just created one more record and it takes the subsequent number. What puzzled me is the first auto-number generated through this ended with '12' instead of 1. 

Well it might be because there the count already results in 11 or 12, so that you already had 11 or 12 users with that department? Then for the first new user, it would indeed start with that instead of 1.

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

LinkedIn