Generate unique Number when field is empty

cipla
Giga Contributor

Hello all,

I have made a table App Number, and its form has a field App No. My requirement is that when any record is created with its App No. field entry as empty, a unique number is generated for all empty entries entries in sequential order.

find_real_file.png

As per the above screenshot , in all records which are empty I want some number to be auto generated. Like for First empty entry Number is EMP1 for second its EMP2 and so on.. and the number should not be duplicated, they must be unique.

Can anybody help me with this requirement?

1 ACCEPTED SOLUTION

Hi Arpita,



Thank you for let me know about the bug in the code.


This happened because of the order by of string field.


Here is the revised version of the BR-



(function executeRule(current, previous /*null when async*/) {


//selecting max number


var curr_max_num=0;


      var next_num=0;


      var gr = new GlideRecord('u_app_number');  


      gr.addQuery('u_app_no','CONTAINS','EMP-');  


      gr.query();


while(gr.next())


{


var max_str=gr.u_app_no;


                      var array = max_str.split('-');


                      var recnum=parseInt(array[1]);


if(recnum>curr_max_num)


curr_max_num= recnum;


}


var nxt_val=curr_max_num+1;


var nxt_val_str='EMP-'+nxt_val;


current.u_app_no=nxt_val_str;


})(current, previous);


View solution in original post

16 REPLIES 16

Suseela Peddise
Kilo Sage

Hi Arpita,



Please check out the below link for detailed information:



Managing Record Numbering - ServiceNow Wiki .



Hope this might be helpful.



hello Suseela,


I have referred the above link but it is not helpful in meeting my requirements.


Harish KM
Kilo Patron
Kilo Patron

Go to your table and enable auto number with your prefix as EMP. SCreen shot below



find_real_file.png


Regards
Harish

cipla
Giga Contributor

I have tried this but it is creating a number for every new record irrespective of the entry in App No. field. I want a number to be generated only when Field is empty else no number should be generated.