spSaved vs spBeforeSave

I am getting varying results from using these two methods.

What I am testing is the idea of a "notification list", that is basically an email list. It has a Subject (Title), To (String), Body(String), and Parent (Lookup to the origin list).

In spBeforeSave, I can composed the Subject, To and Body just fine but I can't use the Parent since I don't have the fd.ItemId yet.

So, I moved to the spSaved method but I don't have access to any of the origin form's fields, so I do a lookup:

fd.spSaved(function(result) {
    sp.web.lists.getByTitle('Origin List').items.getById(result.Id).get().then(function(item){
        sp.web.lists.getByTitle('Notification List').items.add({
            Title: item.Title,
            To: item.Email,
            Body: stringVariable,
            ParentId: result.Id   
        });
    });
});

The problem is, sometimes it works just fine and other times it doesn't work at all.

I tried trapping it in a Try/Catch but I never get an error in the Catch. I user alert(JSON.stringify()) on the "result" and the "item" variables, I always get the "result", I only occasionally get the "item".

Any ideas?

Hello @smithme,

To make sure that the pnp request is completed, you need to disable the redirection and then redirect once the operation is completed.

fd.spSaved(function(result) {
  var redirect = result.RedirectUrl;
  result.RedirectUrl = null;
  var siteURL = 'https://abcd.sharepoint.com/sites/ProjectsDashboard';
    sp.web.lists.getByTitle('Origin List').items.getById(result.Id).get().then(function(item){
        sp.web.lists.getByTitle('Notification List').items.add({
            Title: item.Title,
            To: item.Email,
            Body: stringVariable,
            ParentId: result.Id   
        });
    }).then(function(){
    console.log("item created!");
    window.location.href = redirect;
  });
});
1 Like

This is not working for me.

Setting the RedirectUrl to null isn't stopping the redirect.

What is the variable siteURL for?

I get the alert for "result".
I do not get the other two alerts.

Here is my code:

var redirect = result.RedirectUrl;
redirect.RedirectUrl = null;

alert(JSON.stringify(result));
sp.web.lists.getByTitle('Notification Test List 1').items.getById(result.Id).get().then(function(item) {
    alert(JSON.stringify(item));
    var body = '<h1>' + item.Title + '</h1><p><a href="https://www.yahoo.com/">Test Link</a></p>';
    sp.web.lists.getByTitle('Notification Test List 2').items.add({
        Title: item.Title,
        To: 'myemail@email.com',
        Body: body,
        ParentId: result.Id
    });
}).then(function() {
    alert('Here');
    window.location.href = redirect;
});

@smithme,

How do you open the form? In a full screen, in panel or in dialog window?

Full screen.

I haven't tried it on a panel or a dialog window.

Hello @smithme,

I just noticed that you've shared just a part of the code. Under what event are you running the code?

This code should be placed within spSaved event handler.

Also, the result object is not available within the add a new item function, you need to replace it with item.Id.

var redirect = result.RedirectUrl;
redirect.RedirectUrl = null;

alert(JSON.stringify(result));
sp.web.lists.getByTitle('Notification Test List 1').items.getById(result.Id).get().then(function(item) {
    alert(JSON.stringify(item));
    var body = '<h1>' + item.Title + '</h1><p><a href="https://www.yahoo.com/">Test Link</a></p>';
    sp.web.lists.getByTitle('Notification Test List 2').items.add({
        Title: item.Title,
        To: 'smithme@cityofnampa.us',
        Body: body,
        ParentId: item.Id
    });
}).then(function() {
    alert('Here');
    window.location.href = redirect;
});

If it still doesn't work, You can also try to add a debugger to your code to follow it step by step and see where the error is:

debugger;

Here is the whole function:

fd.spSaved(function (result) {
    var redirect = result.RedirectUrl;
    
    redirect.RedirectUrl = null;
    alert(JSON.stringify(result));
    sp.web.lists.getByTitle('Notification Test List 1').items.getById(result.Id).get().then(function(item) {
        alert(JSON.stringify(item));
        var body = '<h1>' + item.Title + '</h1><p><a href="https://www.yahoo.com/">Test Link</a></p>';
        sp.web.lists.getByTitle('Notification Test List 2').items.add({
            Title: item.Title,
            To: 'myemail@email.com',
            Body: body,
            ParentId: result.Id
        });
    }).then(function() {
        alert('Here');
        window.location.href = redirect;
    }); 
});

I caught the bug that doesn't stop the redirect, I was nulling the RedirectUrl on the variable "redirect" not "result".

The redirect is now stopped. Thanks.

1 Like