Skip to main content

Search

How populate NewForm with auto incremented number or counter BEFORE saving item? - e.g. new order number

Comments

1 comment

  • ArdeviaForum

    Original Post by Vitaliy Zadorozhnyy (Imported from Ardevia Forum)

    Hi Alexander,
    You can set generated value on new form before saving item. For this you have to use context function

    [code=plain]?[[@Web.GetFirstValueForQuery('ListName', 'Caml query', 'FieldName')]] 
    

    in the script action on the save button.

    Try to follow next steps to setup required solution:

    1. Add field which contains generated value to the form.
    2. Hide it, by setting visible expression to =false. (As this field value should be set just before saving item)
    3. Edit actions for save button, by selecting Behaviour Tab and then double click on save button to see configured actions.
    4. Press 'Edit' button on Behaviour Tab to edit save button actions.(By default there are Save Form and Redirection actions).
    5. In the appeared Action Builder Dialog add new action and move it to the top by drag and drop or by up button.(This new action has to be placed before save action as it is required to set generated value before saving item).
    6. Select Type of New action to Execute Script.
    7. Paste following script into the script body area:
      var lastNumber = [[@Web.GetFirstValueForQuery('YourListTitle',  '<View><Query><OrderBy><FieldRef Name="YourFieldName" Ascending="FALSE"/></OrderBy></Query></View>', 'YourFieldName')]];  
      var inputController = jQuery('span.ms-formbody[fieldname="YourFieldName"]').closest('.ard-formfield').data('Controller');
      inputController.InstanseData.SetValue(lastNumber+1)
      by replacing YourListTitle with correct list title and YourFieldName with field name you added and hidden in steps 1,2.
    8. Close Action Builder Dialog by pressing save button then save the form and test saving of new item.

    A short description to the script:

     

    Line 1: define CAML query to get items ordered by descending. You can modify it to meet various requirements.

     

    Line 2: set variable lastNumber to first value of evaluated query(ordered list items by generated field value).

     

    Lines 3,4: get handle to field input and set value to lastNumber +1.

    Hope this solution will meet your requirements.

    0

Please sign in to leave a comment.