Calcul on decimal field - Issue with dot and comma

abaron
Mega Guru

Hello,

I'm on Geneva release.

I have a decimal field on my child table and a other one on my parent table (Total).

I try to do the SUM of all childs records and push the result on the parent record (Total)

On the child table I have created a Business Rule executed after the insert/update/delete.

The BR run a script who calcul the SUM (using GlideAggreate) and push it to the parent record.

BR script:

#############################

(function executeRule(current, previous /*null when async*/) {

  var totalamount = 0;

  // Sum amount of all active records with the same parent

  var gr = new GlideAggregate('u_mychildtable');

  gr.addQuery('u_active',true);

  gr.addQuery('u_parent',current.u_parent);

  gr.addAggregate('SUM', 'u_amount');

  gr.setGroup(false);

  gr.query();

  if (gr.next()) {

          totalamount = gr.getAggregate('SUM', 'u_amount');

  }

  // Update u_total_amount on parent quotation record

  gr = new GlideRecord('u_myparenttable');

  gr.get(current.u_parent);

  gr.u_total_amount = parseFloat(totalamount);

  gr.update();

})(current, previous);

#############################

It works, when i update a child record from the related list of the parent record, the BR run and the total is automatically updated on the screen of the parent record. But it is a dot that is used for the decimal separator instead of a comma, so if i try to save the parent record i have an error :

find_real_file.png

if i replace the dot by a comma or if i close and reopen the parent record, the system replace the dot by a comma and i can modify and save it.

find_real_file.png

How can i manage this problem ?

Thank you by advance for your help.

2 REPLIES 2

Midhun1
Giga Guru

Use sub-string and remove dot.


staceyrocheleau
Kilo Guru

That is really odd...not understanding why it likes a comma but not a dot.



You can replace the dot with a comma if you need to:


parseFloat(totalamount.replace(/[.]+/g,",");



The only issue is if there is a comma for the thousands place (82,000.50) trying to change this to 82.000,50 can be trickier and will have to involve replacing many things:


totalamount.replace(/[,]+/g,"_"); --> (/[.]+/g,","); --> (/[_]+/g,".");



But not really sure why a decimal field would have an issue with 82.5 as I can set a field to 82.5 no problem. Is it a system setting or country specific setting you put in your instance? (How/where can I change the decimal notation?   or Decimal field not playing ball and showing decimals )



What happens if you put 82.5 in that field yourself and save it? Does it accept you writing it with a decimal?



What if you use Number(totalamount); instead?



If you are using commas instead of periods for your notation, are you running the aggregate query with a user who has that set? The account making the update should be in the right country code.