Auto fill a field in user form

Bruce lee
Tera Contributor

 ->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);

 

 
     

 

1 ACCEPTED SOLUTION

@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.

Arun_S1_0-1686923889644.png

 

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.

 

View solution in original post

5 REPLIES 5

Arun_S1
Tera Guru
Tera Guru

@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".

Arun_S1_0-1686909323370.png

 

5. Create new sys_user records to see the number generation is automatically handled by ServiceNow.

Arun_S1_1-1686909434222.png

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

 

 

Riya Verma
Kilo Sage
Kilo Sage

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.

  1. navigate to "System Definition" -> "Number Maintenance" ->  ,  create a new record.

  2. 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).

  3. 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.

  4. save the record and test it.
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Hi @Bruce lee ,

 

If it works for you , can you please accept the solution so that thread is closed. 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

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