Auto Increment Numerical Value in Catalog Item Variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2021 10:28 PM
Hello Everyone.
I'm having the following requirement on Catalog Item Variable Form. There is a Variable named as Serial Number. Every time a catalog item is requested, the field needs to be auto-populated.
We have to populate the variable with the following information - ('First letter of first name of Requestor' + ' Last letter of first name Requestor' + 10500).
The number is starting with 10500 and every time the Catalog item is submitted the number is auto incrementing and is unique (should not be repeated).
Eg - Requestor Name is JACK. So the serial number would would be JK105001. Next time the catalog item is requested the serial number should be **105002,**105003 and so on.
NOTE - The Variable 'Serial Number' is not mapped to any field on RITM. IS it possible to query variables for a catalog item in SN ?
Please suggest the best way to do this. Thanks in Advance.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2021 11:04 PM
Ok, hold your breath... this is a biggie...
Client Script:
function onLoad() {
var ga = new GlideAjax('SerialNumberUtils');
ga.addParam('sysparm_name', 'getExistingSerialNumber');
ga.addParam('sysparm_user_name', g_user.userID);
ga.getXML(getResponse);
}
function getResponse(response) {
var firstName = g_user.firstName.toString();
var firstChar = firstName.charAt(0);
var lastChar = firstName.charAt(firstName.length - 1);
var serialNumber = response.responseXML.documentElement.getAttribute("answer");
if (serialNumber == '')
serialNumber = firstChar + lastChar + '105001';
else {
var increment = parseInt(serialNumber.toString().charAt(serialNumber.length - 1)) + 1;
serialNumber = serialNumber.substr(0, serialNumber.length - 1) + increment;
}
alert('finally we get - ' + serialNumber);
}
Script Include:
var SerialNumberUtils = Class.create();
SerialNumberUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getExistingSerialNumber: function() {
var serialNumber = '';
var ritm = new GlideRecord('sc_req_item');
ritm.orderByDesc('sys_created_on');
ritm.addQuery('requested_for', this.getParameter('sysparm_user_name'));
ritm.query();
if(ritm.next())
{
serialNumber = ritm.variables.serial_number;//Repalce your field name here
}
return serialNumber;
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2021 11:05 PM
The script takes into consideration that there's a field on your catalog called serial_number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2021 11:14 PM
Hi Anirudh. Thanks for brining that up.
I don't have a field on the RITM form named as "serial_number', but I have a catalog variable on the catalog item form that is available on the portal named as 'serial_number'. My requirement is to auto populate that variable on submit. It's not mapped anywhere on the RITM table. Just a catalog form variable.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2021 11:17 PM
Perfect! This script 'looks' exactly for such a variable on the catalog form.
Also, i just realized I missed a query in the script include, see updated code snippet:
var ritm = new GlideRecord('sc_req_item');
ritm.orderByDesc('sys_created_on');
ritm.addQuery('item.name' , 'Your Item Name');//change the name
ritm.addQuery('requested_for', this.getParameter('sysparm_user_name'));
ritm.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2021 11:46 AM
Hi Anirudh,
By using the given code(in addition to the mentioned script include) I'm able to get the answer in Alert but unable to set the value in the desired variable. Can you please help what I'm doing wrong.
Thanks in Advance.
function onSubmit() { function getResponse(response) { var firstName = g_form.getValue('first_name'); var firstChar = firstName.charAt(0); var serialNumber = response.responseXML.documentElement.getAttribute("answer"); //alert('finally we get - ' + serialNumber); g_form.setValue('serial_number', serialNumber); |