Number Maintenance - Adding a Year (YYYY) to the Prefix of a Table

Munna1
Tera Contributor

While creating new Request it must follow this format REQF-SN-YYYYXXXXX.

   Here YYYY denotes the Current Year. For e.g. 2024. Kindly note that this should get incremented as the year changes.

Munna1_0-1706502444061.png

   XXXXX would be a five digit auto incremented value. Here, this series should start from ‘10000’ series and get incremented by count of 1.This should get reset after the end of the year.
     e.g. Jan 1 2024 entitlement should be like -> REQF-SN-202410001

     and Jan 1 2025 entitlement should be like -> REQF-SN-202510001

 

I am trying with the Before insert BR. But it is not working as expected.

I will appreciate the explanation with the Script.

Thanks In Advance.

1 ACCEPTED SOLUTION

Hi @Munna1 
You can reset the "sys_number_counter" within the same scheduled job as mentioned by @Tai Vu 

Sample Script:

 

var gdt=new GlideDateTime();
var t=new GlideRecord('sys_number');
t.addEncodedQuery('category=your_table_name');
t.query();
if(t.next())
{
	t.prefix="REQF-SN-"+gdt.getYear();
	t.update();
	var tt=new GlideRecord('sys_number_counter');
	tt.addEncodedQuery('table=your_table_name');
tt.query();
if(tt.next())
{
	tt.number=0;
	tt.update();
}

}

 

 

C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.


Thanks and Regards,

Saurabh Gupta

View solution in original post

6 REPLIES 6

Tai Vu
Kilo Patron
Kilo Patron

Hi @Munna1 

You may need a scheduled job that runs on the 1st of Jan Yearly to update the prefix in the Number Maintenance.

 

Cheers,

Tai Vu

Munna1
Tera Contributor

Hi Timi,

Thanks for your response.

 

How can we reset these five digits auto incremented value to 10,000 again after year changes?

 

I will appreciate the explanation with the Script.

Thanks In Advance.

Hi @Munna1 
You can reset the "sys_number_counter" within the same scheduled job as mentioned by @Tai Vu 

Sample Script:

 

var gdt=new GlideDateTime();
var t=new GlideRecord('sys_number');
t.addEncodedQuery('category=your_table_name');
t.query();
if(t.next())
{
	t.prefix="REQF-SN-"+gdt.getYear();
	t.update();
	var tt=new GlideRecord('sys_number_counter');
	tt.addEncodedQuery('table=your_table_name');
tt.query();
if(tt.next())
{
	tt.number=0;
	tt.update();
}

}

 

 

C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.


Thanks and Regards,

Saurabh Gupta

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Through a Scheduled Flow which runs every Jan 1 at midnight, you could update:

sys_number record for your table and update the prefix
+
sys_number_counter record for your table and reset it to 0 or 10000

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn