Updating a child list not updating when using a variable

Hello,

I would like to check if someone is having the same behavior or I where did I go wrong on this code. I am trying to update a child list item on save. The update works if I hardcode the item id but not working if I use the following code below.

fd.spBeforeSave(function () {
    var nextID = fd.itemId;
    pnp.sp.web.lists.getByTitle("Test").items.filter("sourceID eq " + nextID.toString()).get().then(function (item) {
        console.log(item);
        pnp.sp.web.lists.getByTitle('Test').items.getById(item[0].Id).update({         
            Title: fd.field('PrepaidName').value,
            APPrep2: 'testing',
            sourceID: nextID.toString()
        });
    });
    
});

Dear @Mars_Basmayor,
This function is async, so it might not have enough time to run before save - try it outside of before save event. Also, you can try storing it in variable, and then updating without toString():

    var nextID = fd.itemId;
    pnp.sp.web.lists.getByTitle("Test").items.filter("sourceID eq " + nextID.toString()).get().then(function (item) {
        console.log(item);
        pnp.sp.web.lists.getByTitle('Test').items.getById(item[0].Id).update({         
            Title: fd.field('PrepaidName').value,
            APPrep2: 'testing',
            sourceID: nextID
        });
    });

Hello @Nikita_Kurguzov,
I tried the code outside the before save event and works fine. Thanks for your inputs. I might as well change to an on.change event to update the value directly.

1 Like