How to create a custom unique ID for every new list item/form request
Last Update: March 19, 2018
Suppose you want to generate a custom unique ticket ID when saving the form where the Ticket ID is in the format of [T][Year]-[Integer Serial] and the [Integer Serial] part starts with 1 and resets to 1 every year. For example, T17001, T17002, … T17090 and when 2017 ends, the integer part reverts to 1 so in 2018 the ticket ID's would look like T18001, T18002, … T18200.
Here are the steps on how to do that, assuming that your list name is "Tickets".
- Open the form in
design mode.
- Create three columns in your list and name them as follows:
- TicketID: with type "Single line of text".
- Year: with type "Single line of text".
- SID: with type "Single line of text".
- Map the above created columns to the form with controls or form variables. Herein we will assume you are going with the controls option.
- Select the Form by clicking on any empty space on the form design workspace area.
- In the
Rules pane, click on the Add icon to add a new rule, the
Rule Manager dialog will open.
- Add a new rule as follows:
A. Change the
Rule Name as desired.
B. Change the
Rule Type to Action.
C. Change the
Event Type onSave.
D. In the Condition section, type the following script in order to make sure that the action runs only on new items.
getFormQueryString('type') == 'New'
E. In the Action section, type the following script:
var d=new Date().getFullYear()
var SIDArray=getListFieldData('/','Tickets','SID', "<Query><Where><And><IsNotNull><FieldRef
Name='ID' /></IsNotNull><Eq><FieldRef Name='Year' /><Value Type='Text'>" +d
+ "</Value></Eq></And></Where></Query>")
var newSID=1;
if(SIDArray.length==0){
newSID=1
}else{
var LastSID=SIDArray[(SIDArray.length)-1]
newSID=parseInt(LastSID)+1
}
var format='T'+d.toString().substring(2,4)+'-'+newSID
setValue(TicketID,format)
setValue(SID,newSID)
setValue(Year,d)
F. Click on the
Save button to save the rule.
Note: You can use
Assistance Panel to help you adding functions and related parameters, form variables and form controls to
Conditions and/or
Actions sections. - Test the form by publishing it and adding a new item to the list.