how to fetch the latest record number using java script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 02:37 AM
I want to get latest number of user table and assign the next number to new user. How to write in java script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 10:37 AM
@Mrunal Deshmukh What do you mean latest number in user table we don't have auto numbering you can sort with any field.
The orderBy() and orderByDesc() methods both accept one argument: The name of a field in the database table specified when instantiating the GlideRecord object you're calling this method on. This is the field that will either be sorted low-to-high/a-to-z/past-to-present if you used the orderBy() method, or the opposite if you called orderByDesc().
var gr = new GlideRecord('sys_user');
gr.addQuery('active',true);
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
//records will come in order of latest created
}
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 10:46 AM
@Mrunal Deshmukh What do you mean latest number in user table we don't have auto numbering you can sort with any field.
The orderBy() and orderByDesc() methods both accept one argument: The name of a field in the database table specified when instantiating the GlideRecord object you're calling this method on. This is the field that will either be sorted low-to-high/a-to-z/past-to-present if you used the orderBy() method, or the opposite if you called orderByDesc().
var gr = new GlideRecord('sys_user');
gr.addQuery('active',true);
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
//records will come in order of latest created
}
Bharath Chintala

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 11:20 AM
In a Business Rule, you can use the getNextObjNumberPadded() function.
See https://www.servicenow.com/community/developer-forum/import-set-auto-numbering-when-field-is-blank/m...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2023 03:35 AM - edited 02-04-2023 03:36 AM
You can write a JavaScript function in ServiceNow to get the latest number of the user table and assign the next number to a new user by following these steps:
- Create a Server-side JavaScript include.
- In the include, write a function to retrieve the latest number of the user table using a GlideRecord query.
var gr = new GlideRecord("sys_user");
gr.orderByDesc("number");
gr.query();
gr.next();
var latestNumber = gr.getValue("number");
3. Increment the latest number to get the next number
var nextNumber = parseInt(latestNumber) + 1;
4. Create a new user record using a GlideRecord and set the next number for the user
var newUserGR = new GlideRecord("sys_user");
newUserGR.initialize();
newUserGR.setValue("number", nextNumber);
// set other required fields for the user
newUserGR.insert();
This function will retrieve the latest number of the user table, increment it to get the next number, and create a new user record with the next number. You can call this function when you want to create a new user.
Anshu