Jim Coyne
Kilo Patron

Ever need an auto-incrementing number for a custom string field?  Just like the OOB Number field on the Task tables?  As usual, the simplest way is to leverage OOB functionality.

1. Create a new custom table:

find_real_file.png

Uncheck the "Create module" field because you won't really need access to the table - we will NOT be creating any records in it.  Add a new String field called "Number".

You can either use that new table as is, or as I like to do, use the new one as the Parent and create Child tables to do the actual counting.  I usually do it this way because you just know if you need 1 counter, you'll need another at some point.  That's why I check the "Extensible" field on this table and create child tables, keeping them together and sharing the one "Number" field.

 

find_real_file.png

 

2. Create another table, extending it from the previous "Custom Counter Base" table:

find_real_file.png

Again, you can uncheck the "Create module" field.  Check the "Auto-number" field and fill in the details of the number you need created.  That will create the "Number" record which will do all the work for you.

Now that you have the tables setup, you can then use the following script to create new numbers:

var nm = new NumberManager("u_custom_counter_item");
var number = nm.getNextObjNumberPadded();

or chain it all together

var number = new NumberManager("u_custom_counter_item").getNextObjNumberPadded();

or set a field's default value

javascript:new NumberManager("u_custom_counter_item").getNextObjNumberPadded();

 

Creating the tables is unfortunately only required in order to create the Number record as it now has a reference field to the "Table" table.  You used to be able to enter names in a plain string field so no new tables were required to add this functionality.  A little bit of overhead, but nothing that will affect the platform.  Oh, well, progress and all.  🙂

Note: setting a Service Catalog variable's "Default value" field to create a new value will increment the number counter by 2 for some reason.  And if used in a Service Portal, the number jumps by 8.  Not sure why this is happening, but you can use a GlideAjax call in an onLoad Catalog Client Script to get the number and have it increment properly by just 1.

Here's what your Script Include would look like:

var CustomAjaxUtils = Class.create();
CustomAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCounterNumber: function() {
		var dataToReturn = {};
		var tableName = this.getParameter("sysparm_table_name");
		if (tableName){
			var nm = new NumberManager(tableName);
			dataToReturn.number = nm.getNextObjNumberPadded();		
		}
		return JSON.stringify(dataToReturn);
	},

	type: "CustomAjaxUtils"
});

And your Catalog Client Script:

function onLoad() {
	//get a new number
	var ga = new GlideAjax("CustomAjaxUtils");  //name of the Script Include
	ga.addParam("sysparm_name", "getCounterNumber");  //name of the function we want to call
	ga.addParam("sysparm_table_name", "u_custom_counter_item");
	ga.getXML(u_displayData);

	function u_displayData(response){
		var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));  //convert the returned JSON string to an object
		g_form.setValue("number", answer.number);
	}
}

Set the Catalog Client Script to only run on the Catalog Item view so the number is only generated when ordering the item.

2 Comments