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

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

Geetanjali Khy2
Mega Guru

Hi,

 

Try with adding gr.update() at the end.

You can refer below link it will help,

https://community.servicenow.com/community?id=community_question&sys_id=dc764fe1db1cdbc01dcaf3231f96...

 

Kindly mark the answer as Correct and Helpful if this answers your question.

Thank You.

 

Regards,

Geetanjali Khyale

I have added gr.update(); but it is not triggering the number