How to change a field using a button

I am using a button to try and change a field called "Status" on my form. I tried some code on that button, but could not make it work. This is what I have so far for the button's on click code:

fd.spSaved(function(result) {
fd.spRendered(function() {
fd.field('Status').value = 'Approved';
});
});

return fd.save();

Can you let me know what I might be doing wrong?

Hi @zddummer,

Could you share more on your use case? Do you need to save the form when the button is pressed?

If your goal is to simply change the Status field, try using

fd.field('Status').value = 'Approved';

I want the button to update the field and close out of the form. Would it just be this then?

fd.field('Status').value = 'Approved';

return fd.save();

Hi @zddummer,

Exactly.

The spRendered function is called only once when the form loads up. If you update that function in the code that is executed upon clicking the button, the function will never run.

Just put the lines you need executed into the Click property of the button.

I have put this code into the Click property of the button, but now when I click the button, it doesn't do anything:

fd.field('Status').value = 'Approved';
return fd.save();

Hi @zddummer,

Could you share the screenshots of the browser console (F12)?

Sorry here is a csv of everything. Didn't realize there was more than what I provided in the screenshot
spfx_trace_logs.csv (10.5 KB)

Hi @zddummer,

I don't see any related errors in the logs.
Could you try putting each of the following snippets into a separate button and pressing all buttons?

return fd.save();
console.log("Button 2 was pressed");
console.log("Button 3 was pressed");
return fd.save();
fd.field('Status').value = 'Approved';

Let me know how it goes.

I figured out the issue. I was opening the Display form and not the Edit form so it wouldn't save the changes to the Status field when the button was clicked. It is working now!