Before Save not populating Column

I reviewed the following article:
Default Column Value not appearing in Form, and unable to run Async PnP in BeforeSave - Forms - Plumsail Community

I am having a similar issue that was described but I have added the

return fd._vue.$nextTick();

I have tried is a few different areas with no success. Here is my code:
fd.spBeforeSave(function(spForm) {
var list = pnp.sp.web.lists.getByTitle("Master%20Log");
var projNum = fd.field('Project_x0020__x0023_').value.LookupValue;
var nextPO = '';
var PONumwPadding = '';
list.items.filter("Title eq '" + projNum + "'").get().then(function(items){
if (items.length > 0) {
var lastPO = items[0].LastPONumber;
var MasterLogID = items[0].ID;
nextPO = Number(lastPO) + 1;
list.items.getById(MasterLogID).update({
LastPONumber: nextPO
}).then(i => {
console.log(i);
if (nextPO < 10) {
PONumwPadding = "00" + nextPO;
} else if (nextPO < 100 && nextPO > 9) {
PONumwPadding = "0" + nextPO;
} else if (nextPO > 99) {
PONumwPadding = nextPO;
}
fd.field('Title').value = projNum + "-" + PONumwPadding;
// tried return fd._vue.$nextTick(); here
});
// tried return fd._vue.$nextTick(); here
} else {
list.items.add({
Title: projNum,
LastPONumber: 1
}).then(function(){
console.log("Created!");
PONumwPadding = "001";
fd.field('Title').value = projNum + "-" + PONumwPadding;
// tried return fd._vue.$nextTick(); here
});
// tried return fd._vue.$nextTick(); here
}
return fd._vue.$nextTick();
});
});

It should be noted the other list is being updated properly.

This is just me guessing, but you also need to specify and return the whole thing as one encapsulation. Only then does the next 'tick' become possible.

I haven't had time to test this, but I slightly amended your code:

fd.spBeforeSave(function(spForm) {
return sp.web.lists.getByTitle("Master%20Log").then(function(list){
var projNum = fd.field('Project_x0020__x0023_').value.LookupValue;
var nextPO = '';
var PONumwPadding = '';
list.items.filter("Title eq '" + projNum + "'").get().then(function(items){
if (items.length > 0) {
var lastPO = items[0].LastPONumber;
var MasterLogID = items[0].ID;
nextPO = Number(lastPO) + 1;
list.items.getById(MasterLogID).update({
LastPONumber: nextPO
}).then(i => {
console.log(i);
if (nextPO < 10) {
PONumwPadding = "00" + nextPO;
} else if (nextPO < 100 && nextPO > 9) {
PONumwPadding = "0" + nextPO;
} else if (nextPO > 99) {
PONumwPadding = nextPO;
}
fd.field('Title').value = projNum + "-" + PONumwPadding;
// tried return fd._vue.$nextTick(); here
});
// tried return fd._vue.$nextTick(); here
} else {
list.items.add({
Title: projNum,
LastPONumber: 1
}).then(function(){
console.log("Created!");
PONumwPadding = "001";
fd.field('Title').value = projNum + "-" + PONumwPadding;
// tried return fd._vue.$nextTick(); here
});
// tried return fd._vue.$nextTick(); here
}

	});
	return fd._vue.$nextTick();
});

});

Hopefully a proper Plumsail agent can confirm if I got this right? Does it work @cwalter2?

Thanks @abolam. Unfortunately I am still having an issue. I had to modify the initial encapsulation to allow the form to submit.

Here is where I am at now:
fd.spBeforeSave(function(spForm) {
//alert('Calculating Next PO');
return sp.web.lists.getByTitle("Master%20Log").get().then(function(list){
var list = pnp.sp.web.lists.getByTitle("Master%20Log");
var projNum = fd.field('Project_x0020__x0023_').value.LookupValue;
var nextPO = '';
var PONumwPadding = '';
list.items.filter("Title eq '" + projNum + "'").get().then(function(items){
if (items.length > 0) {
var lastPO = items[0].LastPONumber;
var MasterLogID = items[0].ID;
nextPO = Number(lastPO) + 1;
list.items.getById(MasterLogID).update({
LastPONumber: nextPO
}).then(i => {
console.log(i);
if (nextPO < 10) {
PONumwPadding = "00" + nextPO;
} else if (nextPO < 100 && nextPO > 9) {
PONumwPadding = "0" + nextPO;
} else if (nextPO > 99) {
PONumwPadding = nextPO;
}
fd.field('Title').value = projNum + "-" + PONumwPadding;
});
} else {
list.items.add({
Title: projNum,
LastPONumber: 1
}).then(function(){
console.log("Created!");
PONumwPadding = "001";
fd.field('Title').value = projNum + "-" + PONumwPadding;
});
}
});
return fd._vue.$nextTick();
});
//return fd._vue.$nextTick(); <-tried here too did not work
});

Hi, can you specify what error is happening? Are you familiar with using debugger; in your spBeforeSave handler? If you can see which line is causing the issue that will give you a clue as to what is wrong.

I just noticed that in your code you are using an arrow function. Maybe don't do that and use

}).then(function(i){}

rather than

.then(i => {

That might be ok, but I'm not too sure how ES6 happy this editor is yet.

Hope that makes sense.

Andy

I made the change you suggested above, still was not able to get this to work. I am not familiar with using Debugger. Most of this code I have gotten by looking at what others have done and trying to implement to suit. I appreciate all your help. I am not getting any actual error on save of the form and the remote forms are updated properly, it is just the field is not getting set.

Hello @cwalter2,

I've slightly updated your code. Please test it under spRendered() event to make sure it has no errors. You will see errors, if there are any, in browser console (F12).

Then test it under spBeforeSave(). You can add breakpoints into the code, just place debugger where you want to pause the script. Please find more information about the debugger here.


var projNum = fd.field('Project_x0020__x0023_').value.LookupValue;
var nextPO = '';
var PONumwPadding = '';
var list = pnp.sp.web.lists.getByTitle("Master%20Log");

list.filter("Title eq '" + projNum + "'").get().then(function(items){

    if (items.length > 0) {
        var lastPO = items[0].LastPONumber;
        var MasterLogID = items[0].ID;
        nextPO = Number(lastPO) + 1;

        if (nextPO < 10) {
            PONumwPadding = "00" + nextPO;
        } else if (nextPO < 100 && nextPO > 9) {
            PONumwPadding = "0" + nextPO;
        } else if (nextPO > 99) {
            PONumwPadding = nextPO;
        }

        fd.field('Title').value = projNum + "-" + PONumwPadding;
        list.items.getById(MasterLogID).update({
            LastPONumber: nextPO
        });
    }
    else {
        PONumwPadding = "001";
        fd.field('Title').value = projNum + "-" + PONumwPadding;
        list.items.add({
            Title: projNum,
            LastPONumber: 1
    });
    }
});

I was able to get this to work on SPRendered. However when I do before save I can see it set the field before the form goes off screen however it does not set. It is acting like the field is set to read only but it is not. The only error I get is Refused to load the script 'https://r4.res.office365.com/footprint/v3.2/scripts/fp-min.js'
The remote table does update as well. Also note I had to modify your script from list.filter to list.items.filter.

@mnikitina I have it all squared away. Here is the final code

fd.spBeforeSave(function(spForm) {
var projNum = fd.field('Project_x0020__x0023_').value.LookupValue;
var nextPO = '';
var PONumwPadding = '';
var list = pnp.sp.web.lists.getByTitle("Master%20Log");

return list.items.filter("Title eq '" + projNum + "'").get().then(function(items){

if (items.length > 0) {
    var lastPO = items[0].LastPONumber;
    var MasterLogID = items[0].ID;
    nextPO = Number(lastPO) + 1;

    if (nextPO < 10) {
       PONumwPadding = "00" + nextPO;
    } else if (nextPO < 100 && nextPO > 9) {
        PONumwPadding = "0" + nextPO;
    } else if (nextPO > 99) {
        PONumwPadding = nextPO;
    }
    var setPO = projNum + "-" + PONumwPadding;
    fd.field('Title').value = setPO;
    list.items.getById(MasterLogID).update({
        LastPONumber: nextPO
    });
}
else {
   PONumwPadding = "001";
    fd.field('Title').value = projNum + "-" + PONumwPadding;
    list.items.add({
        Title: projNum,
        LastPONumber: 1
});
}

});
return fd._vuew.$nextTick();
});

1 Like