Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto numbering through business rule

Siri8
Kilo Contributor

How to write a script for auto numbering records through business rule.I have written below script but it is not triggering.

var gr= new GlideRecord('table_name');
gr.orderByDesc('field_name');
gr.query();
if(gr.next())
{
var num= gr.field_name;
var str= num.substring(3);
var str_i = parseInt(str);
str_i++;
gr.num="CI"+str_i;
}

Can you please suggest me

1 ACCEPTED SOLUTION

Try below in background script before updating the same in business rule. Also, your business rule is on which table & when it is executing makes difference. 

var gr= new GlideRecord('incident');
gr.orderByDesc('number');
gr.query();
if(gr.next())
{
var num= gr.number;
var str= num.substring(3);
gs.print(str);
var str_i=Math.floor(str)+1;  
gs.print(str_i);
gr.num="CI"+str_i;
gs.print(gr.num);
}

 

Let me know until what step you get values printed properly in background script.

 

Thanks,

Jaspal Singh

View solution in original post

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

 

For every table created ServiceNow provides an Auto-numbering feature that can be used. All you need is to Configure Table & look for 'Control' tab from the Table.

 

Thanks,

Jaspal Singh

Yes,Actually I want through business rule that is the reason I have written script.

 

In that case don't use parsetInt() use Math.floor(); something like below.

var str_i = Math.floor(str);

 

Thanks,

Jaspal Singh

 

Its not working I have checked it.