JavaScript Button to save form in New mode and reopen form in Edit mode

  • 2 January 2019
  • 2 replies
  • 117 views

Badge

Goal:

  • I want to create a save and continue button on the form that does the following 2 things:
  1. Saves the form
  2. Reopens the form in Edit Mode

Background:

  • I created a form that will only allow one record to have a user's name (which is an uneditable field) and the Monday of the week when the form was created.
  • If a user tries to create a 2nd new form in the same week, the form hides all of the fields and tells the user to edit their previously submitted form for the week (and conveniently includes a hyperlink).
  • I can't use the "Save and Continue" button action because this action keeps the form in "New Mode" rather than change it to Edit Mode, and the rules that I had set up collapse the form & force users to click on the hyperlink.

  • This means that the button action needs to be set to JavaScript, but I need guidance on the following:

            How do I capture the record's ID into a variable using JavaScript upon pressing the button to save the form?


2 replies

Badge +7

The first thought that comes to mind is to query the list and find the max(ID). Then increment it by 1. However, I have no idea if your link is going to work, because even though the link will be created, there may be a delay until the record actually exists. You'll just have to test it and find out.

However, I will add that this still won't be the best solution, because two people filling out the form at the same time will end up having the same URL. The new ID doesn't get generated until the submit button is pressed. If two people open a new form before either submits it, they'll have the same estimated ID. But when person A hits send before person B, they'll actually have that new ID, whereas person B will have the next one after that, meaning their URL will be incorrect.

Userlevel 5
Badge +14

Out of curiosity, what is the purpose of keeping the form open after you have created the new item? I was doing something similar recently and found that it was more confusing for users than the natural experience of being taken to a completely new screen (IE: The List).

That aside, your form should be in Edit Mode once you save the form using the standard 'Save and Continue' button. In the following example I have used the Out Of Box provided Common References in a few Calculated Controls to show each of the three Form States: 

221868_pastedImage_1.png

A New Form will show the following Output: 

221869_pastedImage_2.png

But as soon as I Click on the Save and Continue button, it changes to: 

221870_pastedImage_3.png

Using those Common References you should be able to easily check whether or not the form is in Edit Mode or not. Additionally you can use a formula along the lines of:

isNullOrEmpty({ItemProperty:ID})

to know if the Item has yet to be created. It will evaluate to true if there is no ID and false if there IS an ID (which means that there is an item). 

Hope this helps. 

Reply