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

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

30 Oct 2017

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
ciao,
Ale Stendardo

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

30 Oct 2017

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();
}
Cheers

User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

31 Oct 2017

Hi Nikita,
thanks a lot for your help and suggestions. I will try it immediately.
Ciao
ciao,
Ale Stendardo

User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

02 Nov 2017

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
ciao,
Ale Stendardo

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

02 Nov 2017

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.
Cheers

User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

02 Nov 2017

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.
ciao,
Ale Stendardo

mustangdjb
Posts: 4
Joined: Tue Jan 30, 2018

30 Jan 2018

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

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

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?
Cheers

mustangdjb
Posts: 4
Joined: Tue Jan 30, 2018

19 Feb 2018

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!

bnunweiler
Posts: 11
Joined: Tue Jun 10, 2014

01 Mar 2018

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,

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 10 guests