Spsaved - update same list item

Hello @Muthu,

You can try using setTimeout () method and check if the problem persists.

fd.spSaved(function(result) {
    window.setTimeout(function() {
    var siteURL = 'https://abcd.sharepoint.com/sites/ProjectsDashboard';
    let web = new Web(siteURL);
    web.lists.getByTitle('Projects').items.getById(result.Id).update({
        ProjectID: "PROJ" + result.Id
    })
    console.log("Updated!")
    }, 100)
});
1 Like

Thanks @mnikitina.
When i use debugger, it actually goes into list item update statement with correct Id (result.Id), but the item is not updated.
I wonder, the operation is cancelled by plumsail form before it completes and gets redirected.

I checked, even setTimeout did not work.
It is most likely the form gets redirected without finishing the spSaved function and the list update action is cancelled.

Hello @Muthu,

Dou you have any other code running under spSaved event? Could you please share it.

Are there any flows triggered on items creation?

Hi @mnikitina,
No other code other than updating a field in the same list item. It works 7 out of 10 times, randomly fails.
No flow triggered.
Thanks,
Muthu

Dear @Muthu,
Please, try the following - first disable redirection, then redirect manually once the operation is complete:

fd.spSaved(function(result) {
  var redirect = result.RedirectUrl;
  result.RedirectUrl = null;
  var siteURL = 'https://abcd.sharepoint.com/sites/ProjectsDashboard';
  let web = new Web(siteURL);
  const i = web.lists.getByTitle('Projects').items.getById(result.Id).update({
  ProjectID: "PROJ" + result.Id
  }).then(function(){
    console.log("Updated!");
    window.location.href = redirect;
  });
});
2 Likes

Hi @Nikita_Kurguzov,

This idea works. Great.

Thanks,
Muthu

Hi @Nikita_Kurguzov.
Tried the provided code below but am not getting ProjectID field updated at all. No errors and don't see the console showing "Updated!".

'spSaved' and result object shows in the console before the form is redirected but I don't see 'Updated!' in the console. Any ideas why the ProjectID field is not being updated?

Dear @stormanh,
Something is wrong then. Are you running the form in a panel, or in full page?

Running in full page.

Dear @stormanh,
Okay, and if you replace redirect with this?

window.location.href = 'https://plumsail.com/';

Hi @Nikita_Kurguzov.
with: window.location.href = 'https://plumsail.com/';
it does not redirect to Plumsail homepage.

Dear @stormanh,
That means that the default redirect is not disabled as it should be. Which version are you running? SharePoint Online or SharePoint 2019? What's the version of the app package in the app catalog?

1.8.2 of Designer and the latest app package 1.0.8.1 for SharePoint Online

Dear @stormanh,
Can you send me a screenshot of the webpart and widget version, in dev tools -> sources?
image
Also, any errors in browser's console?

As for the code, I've just tested it - works exactly the same as before. Maybe something is wrong with the field that you try to populate? Try this instead:

fd.spSaved(function(result) {
  var redirect = result.RedirectUrl;
  result.RedirectUrl = null;
  console.log('spSaved');
  console.log(result);
  var siteURL = 'https://abcd.sharepoint.com/sites/ProjectsDashboard';
  let web = new Web(siteURL);
  const i = web.lists.getByTitle('Projects').items.getById(result.Id).update({
  Title: "PROJ" + result.Id
  }).then(function(){
    console.log("Updated!");
    window.location.href = redirect;
  });
});

May have been the field or something, so when I tried with the default Title field it's working!

Thank you!

Hi @Nikita_Kurguzov

Is there a way to get spSaved to work when a child form is opened in a dialog (from list/library control) and upon it being saved be able to then update the corresponding child item field called "InternalRequest" with the value of a SharePoint calculated field "InternalRequestCalculated" in this child list that was opened and saved in a dialog?

Thanks,
stormanh

Dear @stormanh,
You can add an event to parent form, and subscribe to either add/edit of the child list:

fd.control('SPDataTable1').$on('change', function(changeData) {
    if (changeData.type === 'edit') {
      var siteURL = 'https://abcd.sharepoint.com/sites/ProjectsDashboard';
      var web = new Web(siteURL);
      var i = web.lists.getByTitle('Projects').items.getById(changeData.itemId).update({
        Title: "New title " + changeData.itemId
      }).then(function(){
        console.log("Updated!");
      });
    }
});

You'll need to do the calculation by yourself in JavaScript though, no calculated fields.

Hi @Nikita_Kurguzov

I am actually using the same code you provided - thank you! However, it appears the change event handler of the list/library control doesn't get triggered with this code if the child item of the list/library control is edited (opened in dialog). It gets triggered fine when the child list/library control item is edited inline but not when it's edited in dialog mode. Can you confirm this please, as this is what my tests show.

FYI: I have the list/library control set to inline mode and have a custom button to also allow the child list/library control item to be edited in dialog. It's when I edit the child list/library control item using this custom dialog button that I notice the change event handler of the list/library control doesn't get fired.

Thanks,
stormanh

Dear @stormanh,
It should work with default dialog mode, but with custom dialog it might not. For custom dialog, you need to use dialog's callback function, an example can be found here - Managing dialog with JS — SharePoint forms