Set the date field to self-growth

____39
Tera Contributor

Hi all. I create a custom table and there is a u_date field which type is date, I want to set this field to self-growth(if there is a record u_date is  "2023-08-18", when i create new record , the u_date autofill "2023-08-19"), just like incident's number field. Is it possible ? if can ,how to do it? thank you!

1 ACCEPTED SOLUTION

Karthiga S
Kilo Sage

Hi @____39 

 

Sure, you can achieve this by creating a Business Rule in ServiceNow. 

1. Navigate to System Definition > Business Rules.
2. Click on New to create a new Business Rule.
3. Give your Business Rule a name, for example, "Auto Increment Date".
4. In the "When to run" section, select "before" and "insert".
5. In the "Table" field, select your custom table.
6. In the "Script" field, enter the following JavaScript code:

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord(current.getTableName());
gr.orderByDesc('u_date');
gr.setLimit(1);
gr.query();
if (gr.next()) {
var latestDate = gr.u_date.getDisplayValue();
var date = new GlideDate();
date.setDisplayValue(latestDate);
date.addDays(1);
current.u_date = date;
} else {
// Set to default date if no record found
current.u_date = new GlideDate();
}
})(current, previous);


7. Click on Submit to save the Business Rule.

This Business Rule will automatically set the u_date field to one day after the latest date in the table when a new record is inserted. If there are no records in the table, it will set the u_date field to the current date.

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga

View solution in original post

2 REPLIES 2

Karthiga S
Kilo Sage

Hi @____39 

 

Sure, you can achieve this by creating a Business Rule in ServiceNow. 

1. Navigate to System Definition > Business Rules.
2. Click on New to create a new Business Rule.
3. Give your Business Rule a name, for example, "Auto Increment Date".
4. In the "When to run" section, select "before" and "insert".
5. In the "Table" field, select your custom table.
6. In the "Script" field, enter the following JavaScript code:

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord(current.getTableName());
gr.orderByDesc('u_date');
gr.setLimit(1);
gr.query();
if (gr.next()) {
var latestDate = gr.u_date.getDisplayValue();
var date = new GlideDate();
date.setDisplayValue(latestDate);
date.addDays(1);
current.u_date = date;
} else {
// Set to default date if no record found
current.u_date = new GlideDate();
}
})(current, previous);


7. Click on Submit to save the Business Rule.

This Business Rule will automatically set the u_date field to one day after the latest date in the table when a new record is inserted. If there are no records in the table, it will set the u_date field to the current date.

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga

Harish Bainsla
Kilo Patron
Kilo Patron

(function executeRule(current, previous /null when async*) {
var gr = new GlideRecord(current.getTableName());
gr.orderByDesc('u_date');
gr.setLimit(1);
gr.query();

if (gr.next()) {
var latestDate = gr.u_date;
var nextDate = new GlideDateTime(latestDate);
nextDate.addDays(1);
current.u_date = nextDate.getLocalDate();
} else {
// Set initial date for the first record
var initialDate = new GlideDate();
initialDate.setValue('2023-08-18'); // Set your desired initial date here
current.u_date = initialDate;
}
})(current, previous);