Grand Total for Material Request (Purchase Items)

Hi Everyone, we are trying to implement a custom script in ERPNext that sums all the item’s cost in Material Request. We have this script given to us but the ‘Save’ button and ‘not saved’ status keeps appearing and some users on our workflow doesnt have access to ‘save’ (i presume its items access/permission)

frappe.ui.form.on(‘Material Request’, {
onload(frm) {
// Only applicable for documents in Draft - not for submitted or cancelled
// below parameter causes the script to update on page reload/refresh
console.log(“in onload”)
if (frm.doc.docstatus === 0) {
// Move over the Table of Items and get the amount
var len = frm.doc.items.length;
var grand_total = 0;
for (var i = 0; i < len; i++) {
grand_total += frm.doc.items[i].amount;
frm.set_value(‘grand_total’, grand_total);

Thank you in advance!

i have added the code below frm.set_value(‘grand_total’, grand_total);;

it automatically saves the form but im thinking if this is the best way to do this.