Generating Auto numbering based on
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 08:22 AM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 10:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 11:15 AM
One more thing, remove the 'current.update();' as you state the business rule runs on "Before" and for "Insert". See:
and
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2023 10:35 PM
So while using parseInt() that integer value is getting converted to Octal number base 8( from 1004 to 516) ? is it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 12:10 PM
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 ?