Update a record in an onLoad Client Script after setting a field value?

Marcel H_
Tera Guru

I have an onLoad client script that is doing calculations for various currency fields, and then updating a custom total field. I think however that after setting the value the record is still seeing that there is an unsaved change, since navigating away from the record will cause the "Save Changes" popup to appear, even if you've just saved the record (using the update button seems to update and take me back to the list normally).

Even when there are no other manual changes on the form this happens. Is there a way to force the update in an onLoad script since it doesn't like current.update();?

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Try this:

function onLoad() {
	
	var pa = g_form.getValue('payment_amount');
	pa = pa.split(";").pop();
	var co = g_form.getValue('u_change_order_total');
	co = co.split(";").pop();
	var sow = g_form.getValue('u_sow_total');
	sow = sow.split(";").pop();
	var wo = g_form.getValue('u_work_order_total');
	wo = wo.split(";").pop();
	var ea = g_form.getValue('u_ea_total');
	ea = ea.split(";").pop();
	var ao = g_form.getValue('u_ao_total');
	ao = ao.split(";").pop();
	var total = parseCurrency(pa) + parseCurrency(co) + parseCurrency(sow) + parseCurrency(wo) + parseCurrency(ea) + parseCurrency(ao);
	total = total.toFixed(2);
	total = total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	
	var contractTotal = g_form.getValue('u_contract_total');

	// If the contract total value is the same, do not update it. 
	// Otherwise the form will think it has unsaved changes, even if it doesn't
	if (contractTotal != "$" + total) {
		g_form.setValue('u_contract_total', "$" + total);
	}

	function parseCurrency(num) {
		return parseFloat( num.replace( /,/g, '') );
	}
}

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

8 REPLIES 8

Yes, you could do the same check here.

I don't think the .update() call actually does anything if there are no changes, but I could be wrong.

//set the value of the "SOW total" field on the parent contract and update the record
var currentTotalCost = gr.getValue('u_sow_total');
if (total_cost != currentTotalCost ) {
	gr.setValue('u_sow_total', total_cost);
	gr.update();
}

P.S. When updating other records, make sure it is in an 'after' business rule.

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thanks, I realized I did forget to mention that the script is in an after business rule and not a client script. I'll give your suggestion a shot tomorrow and see how it works, I think that it'll do the trick.

Happy to help.

Please mark this one off as correct so it is removed from the list 🙂


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

nataliya_b
Tera Guru
To get rid of pop up, you could add to the end of your client script g_form.submitted = false;