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.

Generating Auto numbering based on

Arnab Bhaumik
Tera Contributor

I have a custom table where a field is there called "function" with different dropdowns (Marketing, Finance, etc) and another string field called "GAP ID". So my requirement is below : 

1. When a new record is created with Function "Finance" GAP ID will be autogenerated like: "FINI001a" where the 001 part will be auto-incremented for the next Finance function record.

2. When a new record is created with Function "Marketing" GAP ID will be autogenerated like: "MAR001a" where the 001 part will be auto-incremented for the next Marketing function record (002,003 etc). 

 

So I have tried with a before-insert business rule with the below details :

var now_GR = new GlideRecord('u_test_cmdb_table');

 now_GR.addEncodedQuery('u_function', current.u_function);
now_GR.orderBy('u_auto_id');
 
  now_GR.setLimit(1);  
  now_GR.query();
 
  if(now_GR.next()) {
 
 
    var newNum = now_GR.u_auto_id;
 
 
      var intg=newNum.slice(-3);
 
        var str= parseInt(intg);
      var str= str+1;
 
    var str1= str.toString();
    var str2= "00" + str1;
current.u_auto_id=str2;
current.update();
 
    }
 
But not working and giving undefined values and nothing is being updated.
5 REPLIES 5

Bert_c1
Kilo Patron

My test case is posted. change the "if" to "while" in line 6 from above, if more than one record matches the query, and remove line for with the 'setLimit()'.  So each record that meets the previous query is returned.