Can I Calculate Negative Values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 07:53 AM
Is it possible to calculate negative values for a total price field?
Idea is adding customer cost plus company cost, which can sometimes be a negative that totals out in a field below. When I have a -20 for customer and -30 for company I want the total to read -50, but it is setting to 50. Currently running an onChange script to calculate totals into a currency type field.
What can I do? Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 09:30 AM
russ.sarbora, i just saw your blog post: Setting Currency Field values via client-side script and this is pretty much exactly what i have going as well, do you know how i can get a negative value to calculate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2018 09:50 AM
Hi All,
Could you please let me know how do I print the negative value in currency field? like $-20, $-400,$-5000 etc.
Quick reply much appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2018 01:06 PM
Hi Paul,
I did some digging on this. I am running a Jakarta PDI, and it looks like this is a bug on ServiceNow's side.
I'll walk you through exactly why this is happening on the ServiceNow side.
When you go to set the value of a currency field:
// Set the value of the field, which I have named u_total_price
g_form.setValue("u_total_price", "-10");
// 1. GlideForm.setValue does some checks that are not relevant
// to this issue and then calls this._setValue
// 2. GlideForm._setValue does some checks that are not relevant
// to this issue and then calls this.elementHandlers[control.id].setValue
// which is CurrencyElement.setValue
// 3. CurrencyElement.setValue checks the format of the string to make sure it
// is in the valid format. Because this technically is not the correct format,
// the string is transformed into currencyCode + ";" + value, which makes my
// value "USD;-10". So far, so good.
// 4. The currency variable is set to the return value of this._extractCurrency
// 5. CurrencyElement._extractCurrency splits the string on ";"
// The resulting array is ["USD", "-10"]
// ** THIS IS WHERE IT BREAKS**
// 6. Element at index 1, "-10", is run through a replace that uses a regular expression
// that replaces anything that is not a number 0 through 9 or a period with an empty
// string, so "-10" becomes "10".
Because of this, the value doesn't appear to be able to be set appropriately by calling g_form.setValue. To accomplish what you want, you'll want to do the following:
// Assuming the field name is u_total_price and the calculated amount is -10.
// Pretend total was set by the calculation
var total = -10;
// Must toString total because setValue attempts to do a split.
g_form.setValue("u_total_price", total.toString());
// Using toFixed just for aesthetics to have it always have 2 decimal places.
gel(g_form.getControl("u_total_price").name + ".display").value = total.toFixed(2);
Hope this helps.