​​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".

  1. Open the form in design mode.
  2. 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".
  3. Map the above created columns to the form with controls or form variables. Herein we will assume you are going with the controls option.
  4. Select the Form by clicking on any empty space on the form design workspace area.
  5. In the Rules pane, click on the Add icon to add a new rule, the Rule Manager dialog will open.
    How to auto-shifting Panel control in a form2.jpg
  6. 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.
  7. Test the form by publishing it and adding a new item to the list.