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

Hi,

Please review the proper syntax/usage of Javascript 'slice()' (negative numbers are not supported that I see). 

 

I don't have your table or fields but a similar test on the 'idea' table and two string fields follow:

 

var now_GR = new GlideRecord('idea');
now_GR.addEncodedQuery('approval=not requested');
now_GR.orderBy('impact');
now_GR.setLimit(1);  
now_GR.query();
if(now_GR.next()) {
  var newNum = now_GR.task_effective_number;
  gs.info("Found record with number: " + now_GR.number + ", newNum = " + newNum);
  var intg=newNum.slice(4);
  var str= parseInt(intg);
  gs.info("intg = " + intg + ", str = " + str);
  str= str+1;
  var str1= str.toString();
  var str2= "00" + str1;
  gs.info("str1 = " + str1 + ", str2 = " + str2);
//  current.task_effective_number=str2;
//  current.update();
}

and the output follows:

*** Script: Found record with number: IDEA0001004, newNum = IDEA0001004
*** Script: intg = 0001004, str = 516
*** Script: str1 = 517, str2 = 00517

Seems parseInt() is assuming some value for the optional "radix" parameter.

Bert_c1
Kilo Patron

Arnab Bhaumik
Tera Contributor

So while using parseInt() that integer value is getting converted to Octal number base 8( from 1004 to 516) ? is it ?  

Arnab Bhaumik
Tera Contributor

Thanks for the update.It worked first time .then not working for next new record. Can you share your test case and update on idea table ?