- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 02:43 AM
->In user form there's a string field named Employee number it should auto fill with a prefix and a number in sequential order when a new user is created.
->For example the last existing user Employee number is GH1000 then when a new user is created its employee number field should filled with GH1001 so it follows in sequential order whenever a new is created
For this I used a business rule but it's but its printing GHundefined please help me to fix this
This is the BR I used in sys_user table
When to run -> Before insert update
(function executeRule(current, previous /*null when async*/ ) {
if (current.isNewRecord()) {
var latestNumber = '';
var existingUsers = new GlideRecord('sys_user');
existingUsers.addNotNullQuery('employee_number');
existingUsers.addQuery('employee_number', 'STARTSWITH', 'GH');
existingUsers.orderByDesc('employee_number');
existingUsers.setLimit(1);
existingUsers.query();
if (existingUsers.next()) {
var lastNumber = existingUsers.employee_number.substr(2);
var nextNumber = parseInt(lastNumber, 10) + 1;
latestNumber = 'GH' + nextNumber.toString().padStart(lastNumber.length, '0');
} else {
latestNumber = 'GH0001';
}
gs.log('Latest Employee Number: ' + latestNumber);
current.setValue('employee_number', latestNumber);
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 07:01 AM
@Bruce lee Navigate to the u_number dictionary entry for the sys_user table, and navigate to Default Value tab and remove the text "javascript:global.getNextObjNumberPadded();" from the default value field.
Create an Before Insert Business rule and write the below script
(function executeRule(current, previous /*null when async*/) {
// Add your code here
current.u_number=global.getNextObjNumberPadded();
})(current, previous);
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 02:57 AM
@Bruce lee, You can use the out of the box number maintenance functionality instead of writing a custom solution. Please follow the steps below.
1. Navigate to System Definition --> Number Maintenance
2. Create a new record with the below details
Table - User (sys_user)
Number- 1000
Prefix - GH
3. Once the above record is created navigate to System Definition --> Tables, search for the sys_user table and view the table definition.
4. In the list of columns, you should see a new column auto generated with the name "u_number".
5. Create new sys_user records to see the number generation is automatically handled by ServiceNow.
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 03:18 AM
Hi @Bruce lee ,
Hope you are doing great.
we dont need to write any script , we have OOB Module present which create a number in sequential manner.
navigate to "System Definition" -> "Number Maintenance" -> , create a new record.
Configure the number maintenance record: Fill in the necessary details for the number maintenance record. Set the "Table" field to the user table (typically "sys_user"), and in the "Start at" field, specify the starting value for the sequential number (e.g., 1001).
Define the prefix: Determine the prefix you want to use for the "Employee number" field. Let's assume the prefix is "GH" for this example.
- save the record and test it.
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 04:58 AM
Hi @Bruce lee ,
If it works for you , can you please accept the solution so that thread is closed.
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 05:52 AM
No @Riya Verma actually the record is created or not simply it's iterating numbers but as per logic it shouldn't for example if I opened user record and clicked new I didn't saved it but it iterated the number for me every record should be in sequential order