Create New Listitem onSubmit of NewForm

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
gaj
Posts: 40
Joined: Wed Feb 01, 2017

01 Aug 2018

Hi,
After creating a new product (Onsubmit - NewForm_product) i have to create a new listitem in a different List(product_xy) and refrence this listitem with the ID from the created product!
(....for each productgroup i have to create a separat list due to the fact i´m having about 500 different product-attributes(columns) which is not possible to store in one List!!!)
I´m able to create the new Listitem (product_xy) with this code:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and then calls createListItem method

function createListItem() {
var clientContext = new SP.ClientContext.get_current();//if the page and the list are in same site.If list is in different site then use relative url instead of get_current
var oList = clientContext.get_web().get_lists().getByTitle('P_RP_Turbine');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Product_LU', ID from newly created product);
oListItem.update();

clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded() {
alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}


My question:

How to get the "ID from newly created product" to pass it to the createListItem()-function ?
..after this i want to redirect to the Edit_Form of product_xy-list

i would do this with:

var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', Edit_Form of product_xy-list.aspx?ID=');
fd.sourceFormParam(uri);


Thank´s for helping me.
Johannes

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

01 Aug 2018

Dear gaj,
One thing you can do is redirect user not to the Edit form of the List, but instead use this code to redirect user to a custom page with your scripts:

Code: Select all

var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'custom-page-with-scripts.aspx?ID=');
fd.sourceFormParam(uri);
On this custom page, you can run the code that will use the URL parameter ID, and do pretty much the same thing that you did before saving an item (create an item), plus it will then redirect user back to the list, to the Edit Form, since the ID is available.

So, what I propose is this structure:

Save an item -> Redirect to custom page with the ID -> Use ID in your script on this custom page -> Redirect user back to Edit form

Hope this helps! Let me know what you think and how it goes.
Cheers

gaj
Posts: 40
Joined: Wed Feb 01, 2017

01 Aug 2018

Nikita Kurguzov wrote:
01 Aug 2018
Dear gaj,
One thing you can do is redirect user not to the Edit form of the List, but instead use this code to redirect user to a custom page with your scripts:

Code: Select all

var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'custom-page-with-scripts.aspx?ID=');
fd.sourceFormParam(uri);
On this custom page, you can run the code that will use the URL parameter ID, and do pretty much the same thing that you did before saving an item (create an item), plus it will then redirect user back to the list, to the Edit Form, since the ID is available.

So, what I propose is this structure:

Save an item -> Redirect to custom page with the ID -> Use ID in your script on this custom page -> Redirect user back to Edit form

Hope this helps! Let me know what you think and how it goes.
Dear Nikita,
thx for your fast replay,

Is it possible to send a second parameter via URL, because depending on the productgroup of the new product I have to redirect them to the according Edit-Form ? See my old code:

fd.onsubmit(function() {
var productGroup = fd.field('Productgroupe').value(); //-->ID
if (productGroup == 1) {
var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'EditForm.aspx?ID=');
fd.sourceFormParam(uri);
}

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

01 Aug 2018

Dear gaj,
Sure, you can try something like this:

Code: Select all

fd.onsubmit(function() {
  var productGroup = fd.field('Productgroupe').value(); //-->ID
  if (productGroup) { 
    var uri = fd.setUrlParam(decodeURIComponent(window.location.href), 'FDRedirectWithID', 'EditForm.aspx?ProductGroup=' + productGroup +'&ID=');
    fd.sourceFormParam(uri);
  }
});
Cheers

gaj
Posts: 40
Joined: Wed Feb 01, 2017

01 Aug 2018

Dear Nikita,

now I have this code in the Form where I´m creating a new Listitem:

// get productgroup and create productgroup-specific-Listitem
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and then calls createListItem method

function createListItem() {
//get product-ID
var productId = GetUrlKeyValue('ID');
var clientContext = new SP.ClientContext.get_current();
// get productgroupe-ID
var url = fd.field('Productgroupe').control()._el().find('a').attr('href');
var pgID = url.substring(url.lastIndexOf("ID=") + 3, url.lastIndexOf("&"));
var oList;
if (pgID == 1) { // Turbine
oList = clientContext.get_web().get_lists().getByTitle('P_RP_Turbine');
}
else {
alert("No list available for this productgroup!");
}
var itemCreateInfo = new SP.ListItemCreationInformation();
oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Product_LU', productId);
oListItem.update();

clientContext.load(oListItem);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
};

function onQuerySucceeded(sender,args) {
fd.sourceFormParam('fd_P_RP_Turbine_EditForm.aspx?ID=' + oListItem.get_id());
};

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
};


The creation of the new Listitem works but the "onQuerySucceeded" or "onQueryFailed" never tiggers!!!
Do you know why?

thx
Johannes

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

01 Aug 2018

Dear Johannes,
Perhaps something as simple as adding () after the name of the function will fix the issue? If not, you can just use it like this:

Code: Select all

clientContext.executeQueryAsync(function() { //your code here });
But if you are using this code not on our form, you won't have access to fd, so be careful with the code. If this is the case, you can simply use window.location.href for redirection.
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 21 guests