Skip to main content

Search

Auto Increment Column - Create Unique ID for item On clicking Save Button

Comments

8 comments

  • ArdeviaForum

    Original Post by iryna (Imported from Ardevia Forum)

    Hello maryb,

     

    Instead of 'Execute Script' action on the save button, you can use 'Set Form File Value' action.

     

    So, steps are the following:
    1. Add field which should contain generated value to the form.
    2. Edit actions for save button: press 'Edit' button on Behaviour Tab.
    3. Add 'Set Form File Value' action and move it to the top by drag and drop or by up button.
    4. Select the field which should contain generated value to the form
    5. Open Expression Builder, select 'Function code' expressions and paste the following code:

    var lastID = [[@Web.GetFirstValueForQuery('YourListTitle', '<View><Query><OrderBy><FieldRef
    Name="YourFieldName" Ascending="FALSE"/></OrderBy></Query></View>', 'YourFieldName')]]; 
    if(lastID){
      var splitedId = lastID.split('-');
      return splitedId[0]+'-'+(Number(splitedId[1])+1);
    }
    return '2015-1';
    

    by replacing YourListTitle with correct list title and YourFieldName with field name you added.
    6. Save the changes.

    0
  • ArdeviaForum

    Original Post by maryb (Imported from Ardevia Forum)

    Many thanks for your help with this.

     

    This code seems to be working great on one list.

     

    On another list I get the initial value for column as 2015-1 but the following error message when I try to add a new item.

     

    Failed evaluate property "Value" in "New Action":Error occured on evaluating expression "{ var lastID = [[@Web.GetFirstValueForQuery('Adverse Occurrence', '', 'itemID')]]; if(lastID){ var splitedId = lastID.split('-'); return splitedId[0]+'-'+(Number(splitedId[1])+1); } return '2015-1'; }"
    Column 'itemID' does not exist. It may have been deleted by another user. /quailty/forms/Lists/Adverse Occurrence

     

    Any help much appreciated.
    Mary

    0
  • ArdeviaForum

    Original Post by Andrii Katsiubka (Imported from Ardevia Forum)

    Many thanks for your help with this.

     

    This code seems to be working great on one list.

     

    On another list I get the initial value for column as 2015-1 but the following error message when I try to add a new item.

     

    Failed evaluate property "Value" in "New Action":Error occured on evaluating expression "{ var lastID = [[@Web.GetFirstValueForQuery('Adverse Occurrence', '', 'itemID')]]; if(lastID){ var splitedId = lastID.split('-'); return splitedId[0]+'-'+(Number(splitedId[1])+1); } return '2015-1'; }"
    Column 'itemID' does not exist. It may have been deleted by another user. /quailty/forms/Lists/Adverse Occurrence

     

    Any help much appreciated.
    Mary

     

     

     

    Hi maryb,

     


    Try to check if field  with name itemID exist in Adverse Occurrence List

    0
  • ArdeviaForum

    Original Post by SPX (Imported from Ardevia Forum)

    When using the following auto increment Code -all works good until I come to 2015-10.
    After that the auto-increment number stays at 2015-10 and will not progress to 2015-11.

     

    Any suggestions.

     

     

    var lastID = [[@Web.GetFirstValueForQuery('YourListTitle', '', 'YourFieldName')]];
    if(lastID){
    var splitedId = lastID.split('-');
    return splitedId[0]+'-'+(Number(splitedId[1])+1);
    }
    return '2015-1';

    0
  • ArdeviaForum

    Original Post by SPX (Imported from Ardevia Forum)

    When using the following auto increment Code -all works good until I come to 2015-10.
    After that the auto-increment number stays at 2015-10 and will not progress to 2015-11.

     

    Any suggestions.

     

    var lastID = [[@Web.GetFirstValueForQuery('YourListTitle', '', 'YourFieldName')]];
    if(lastID){
    var splitedId = lastID.split('-');
    return splitedId[0]+'-'+(Number(splitedId[1])+1);
    }
    return '2015-1';

    0
  • ArdeviaForum

    Original Post by iryna (Imported from Ardevia Forum)

    Please try the following solution:

     

    1. Add two fields which will contain generated values to the form. These should be 'number' and 'simple text' fields.
    2. Edit actions for save button: press 'Edit' button on Behaviour Tab.
    3. Add 'Set Form File Value' action and move it to the top by drag and drop or by up button.
    4. Select the number field.
    5. Open Expression Builder, select 'Function code' expressions and paste the following code:

    var lastID = [[@Web.GetFirstValueForQuery('YourListTitle', '<View><Query><OrderBy><FieldRef 
    Name="YourNumberFieldName" Ascending="FALSE"/></OrderBy></Query></View>', 'YourNumberFieldName')]]; 
    if(lastID){
    return lastID+1;
    }
    return 1;
    

    by replacing YourListTitle with correct list title and YourNumberFieldName with the number field name you added.

     

    6. Add one more 'Set Form File Value' action and move it under the first action.
    7. Select the text field.
    8. Open Expression Builder, select 'Function code' expressions and paste the following code:

     

    var lastID = [[@Web.GetFirstValueForQuery('YourListTitle', '<View><Query><OrderBy><FieldRef 
    Name="YourNumberFieldName" Ascending="FALSE"/></OrderBy></Query></View>', 'YourNumberFieldName')]]; 
    if(lastID){
    return '2015-'+(lastID+1);
    }
    return '2015-1';
    

    by replacing YourListTitle with correct list title and YourNumberFieldName with the number field name you added.

     

    6. Save the changes.

    0
  • ArdeviaForum

    Original Post by maryb (Imported from Ardevia Forum)

    Many thanks for your help.

     

    I am getting the following result after using this code:

     

    2015-2015-91

    0
  • ArdeviaForum

    Original Post by maryb (Imported from Ardevia Forum)

    Apologies,

     

    I had mis-titled columns in CAML query - corrected my error & all is working good now.

     

    Thank you.

    0

Please sign in to leave a comment.