The Zurich release has arrived! Interested in new features and functionalities? Click here for more

how to fetch the latest record number using java script?

Mrunal Deshmukh
Tera Contributor

I want to get latest number of user table and assign the next number to new user. How to write in java script?

 

4 REPLIES 4

BharathChintala
Mega Sage

@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

}

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

BharathChintala
Mega Sage

@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

}

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Nia McCash
Mega Sage
Mega Sage

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

Anshu_Anand_
Kilo Sage
Kilo Sage

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:

  1. Create a Server-side JavaScript include.
  2. 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.

 
Regards,
Anshu