Create Auto-Incrementing Code Numbers (Part 3)

Having created the required Request Number and lookup lists, and added a custom form that will create the new request number items, we are now ready to begin writing our code.

RequestNumbers.js

Our code will be added to the Request Number list’s new item form by attaching a JavaScript include file to a Content Editor web part. To do this, create a document library named Scripts and upload the jQuery and SP Services libraries to it. Make sure the versions of jQuery and SPServices are compatible with each other by reading the Important notes section on the SPServices page. Create a text file and name it RequestNumber.js. At the top of the file, add references to the jQuery and SP Services libraries and a main script tag with the jQuery document ready function.

Create a custom New Item Form

Within the document ready function, start off by declaring the global variables:

Construct the Airport Code drop-down control

Now write a function which uses SPServices to read all of the items in the AirportCodes list. The data needed to construct each option of the drop-down control is contained in the “DisplayName” and “AirportCode” columns. The value of DisplayName will be used as the selectable airport name in the drop-down and AirportCode will be the actual value of the option passed on by the control:

Construct the Department Code drop-down control

Ditto for the Department control. The value of “DepartmentName” serves as the selectable department name in the drop-down and DepartmentCode will be the value of the option posted by the control:

Find the next available code number.

Our request number will begin with a code composed of six letters, three of which correspond to the Airport Code selected by the user in the Airport drop-down control and the other three corresponding to the selected option in the Department control, separated by a comma. These will be followed by the next available number for that particular combination of Airport Code and Department. The first time a request number with a unique letter code is created, the 5-digit number 00001 will be appended to the letter code, e.g. YXX-DEV-00001. The function below determines the next available number by finding all request codes that match the user’s selection of airport code and department (“YXX-DEV”), and counts them in order to determine the next available number.

Add leading zeros to the number

The following function uses recursion to pad the next available number with leading zeros. The “max” variable sets the number of digits desired, which in our case will be set to five.

We now have the functions that will generate the three parts of our Request Number: the three letter Airport Code, the three-letter Department Code, and the five-digit next available number. In our next post we will be writing the functions that read the user input in the drop-down controls and tie the three parts of the Request Number together.

March 26, 2013