
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 02:54 PM
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();?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 04:24 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 02:57 PM
Did you try g_form.save() in the client script?
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 03:29 PM
I tried that, but it ended up creating an endless save/load loop. Below is my current script, I'm sure I'm just missing something simple:
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, ",");
//alert("pa: " + pa);
//alert("tc: " + tc);
//alert("total: " + total);
g_form.setValue('u_contract_total', "$" + total);
//g_form.save();
function parseCurrency(num) {
return parseFloat( num.replace( /,/g, '') );
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 04:24 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2019 06:59 PM
Thanks for the code, that seems to be doing the trick, and helped uncover another issue of a similar nature where I'm updating a parent record when child records are updated or inserted to grab an aggregate sum and then setting the value on the parent record field. Do you think something similar would work to check if the parent field value is the same using GlideRecord? Below is the current script I'm using:
var gr = new GlideRecord('ast_contract');
if (gr.get(current.parent_contract)) {
var ga = new GlideAggregate('u_statement_of_work');
ga.addQuery('parent_contract', current.parent_contract); //find all SOWs with the same parent contract
ga.addAggregate('SUM', 'total_cost');
ga.groupBy('parent_contract');
ga.query();
var total_cost = 0;
if (ga.next()) {
total_cost = ga.getAggregate('SUM', 'total_cost'); //get SUM of all related SOWs
//set the value of the "SOW total" field on the parent contract and update the record
gr.setValue('u_sow_total', total_cost);
gr.update();
}
}