Page 1 of 2

How to trigger Save button on New form and open the Edit Form

Posted: 30 Oct 2017
by AlexStenAlexSten
hello,
is it possible to "force" the click on save button from code, right after opening a New form, so the ID of the item is created, and then redirect to the Edit form of the same item?
I have tried different things, but if I open the New form and I physically click on the save button, it works as expected, savings and opening the Edit form.
If I try to save using code, it save the form and close it, without redirecting.
I'm working on Sharepoint 2013 onPremises, Form Designer ver. 3.1.4.

I started from this post: https://spform.com/buttons/open-edit-fo ... sharepoint
This is the test code I'm using:

vFormType = fd._formType();
if (vFormType = "New")
{
var vTitle = fd.field('Title').value();
if (vTitle == "")
{
fd.field('Title').value('TEMP VALUE');
fd.save().click();
}
}
fd.onsubmit(function() {
var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'fd_Item_Edit.aspx?ID=');
fd.sourceFormParam(uri);
return true;
});
thank you

Re: How to trigger Save button on New form and open the Edit Form

Posted: 30 Oct 2017
by Nikita Kurguzov
Hello, Alex!
Try to switch places between functions in the code, so that onsubmit block loads first and then you click the button.
This code should work:

Code: Select all

vFormType = fd._formType();

fd.onsubmit(function() {
  var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'fd_Item_EditForm.aspx?ID=');
  fd.sourceFormParam(uri);
  return true;
});

if (vFormType = "New"){
  var vTitle = fd.field('Title').value();
  
  if (vTitle == ""){
    fd.field('Title').value('TEMP VALUE');
	fd.save().click();
  }
}
You will also notice I slightly changed code for setUrlParam and replaced default form URI with Forms Designer form. You should also do it if you've changed your Edit form from the default.

Another thing is that you might not need to check the formType if you only add this code to New Form in Forms Designer. In any case, you shouldn't use this code for Edit Form, so it is a bit redundant to check what type of form is opened if you only added this JS to the New Form. Of course, I am not 100% familiar with your scenario and it might be needed for some reason, but in most cases this is unnecessary.

The code should work fine with just that if you only add it to the New form:

Code: Select all

fd.onsubmit(function() {
  var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'fd_Item_EditForm.aspx?ID=');
  fd.sourceFormParam(uri);
  return true;
});

var vTitle = fd.field('Title').value();
  
if (vTitle == ""){
  fd.field('Title').value('TEMP VALUE');
  fd.save().click();
}

Re: How to trigger Save button on New form and open the Edit Form

Posted: 31 Oct 2017
by AlexStenAlexSten
Hi Nikita,
thanks a lot for your help and suggestions. I will try it immediately.
Ciao

Re: How to trigger Save button on New form and open the Edit Form

Posted: 02 Nov 2017
by AlexStenAlexSten
Hi Nikita,
I tried with your code and it save the item as soon I open the New form, but then it fails to redirect to the same item in Edit form.
I'm redirected to the "Page Not Found".
I tried to sort it out, but with no success till now.
I have created a test list and there is no other javascript in the form other than your last suggestion:


fd.onsubmit(function() {
var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'fd_Item_EditForm.aspx?ID=');
fd.sourceFormParam(uri);
return true;
});

var vTitle = fd.field('Title').value();

if (vTitle == ""){
fd.field('Title').value('TEMP VALUE');
fd.save().click();
}

thank you

Re: How to trigger Save button on New form and open the Edit Form

Posted: 02 Nov 2017
by Nikita Kurguzov
Hello, Alex!
Are you sure that you've saved the Edit form in Forms Designer? If not, use this code instead:

Code: Select all

var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'fd_Item_Edit.aspx?ID=');
If neither option works, send us full URL of the page that opens when you get an error, it might help us determine what goes wrong.

Re: How to trigger Save button on New form and open the Edit Form

Posted: 02 Nov 2017
by AlexStenAlexSten
hi Nikita,
yes, the Edit form exist.
I tried with the last code you posted and it works great.

thanks a lot for your support.

Re: How to trigger Save button on New form and open the Edit Form

Posted: 30 Jan 2018
by mustangdjb
I tried all the iterations and the link LOOKS like it should work, but I get page not found.

https://MYSITE.sharepoint.com/HNIDataHu ... aspx?ID=79

Thanks for your help

Re: How to trigger Save button on New form and open the Edit Form

Posted: 31 Jan 2018
by Nikita Kurguzov
Dear Mustang,
What form are you using? Can you send us all the code that you have on it, especially used for redirection?

Re: How to trigger Save button on New form and open the Edit Form

Posted: 19 Feb 2018
by mustangdjb
Nikita Kurguzov wrote:
31 Jan 2018
Dear Mustang,
What form are you using? Can you send us all the code that you have on it, especially used for redirection?

I figured it out. I had the form name slightly incorrect - when I compared the links it became obvious. Solved now!

Re: How to trigger Save button on New form and open the Edit Form

Posted: 01 Mar 2018
by bnunweiler
Hello, I have tried this and it works, however it creates 2 entries and redirects to the second edit form. Can you tell me what I'm doing wrong?

OnPrem 3.1.4

Thanks,