GlideAggregate gets the Older value in Sum - Running the script on before BR

Imran1
Giga Guru

Hi,

 

I have the below BR that aggregates a sum of the field and sets it in another field. However, it doesn't update the latest value but the last entered value to aggregate/sum the value and set it in this new field.

 

For the first 'if' condition I am just setting the value entered in one field to another using GlideAggregate and I can do it directly too (see the commented line)

 

However when I am aggregating the else if part of the code the result is not accurate.  What I am missing?

I believe, as the latest value is not updated/saved in the database the aggregate gets me the value that is already saved. What can I do in this scenario?

 

if (current.document_type == "1") {
var thisRecord = current.sys_id;
var aggsum = new GlideAggregate(current.getTableName());
        aggsum.addQuery('sys_id', thisRecord);
aggsum.addAggregate('SUM', 'not_to_exceed_amount');
        aggsum.setGroup(false);
        aggsum.query();
var aggtotal = 0;
        if (aggsum.next()) {
            aggtotal = parseFloat(aggsum.getAggregate('SUM', 'not_to_exceed_amount'));
            current.total_sow_value = aggtotal;
        }
 
// current.total_sow_value = parseFloat(current.not_to_exceed_amount); Direct value setting
    }
    // else {current.total_sow_value = current.parent_sow.total_sow_value;}
    else if (current.document_type != "1") {
        var aggCO = new GlideAggregate(current.getTableName());
        aggCO.addAggregate('SUM', 'not_to_exceed_amount');
        aggCO.addQuery('parent_sow', current.getValue('parent_sow'));
        aggCO.addQuery('sys_created_on', "<=", current.sys_created_on);
        aggCO.setGroup(false);
        aggCO.query();
var aggTotalCo = 0;
        while (aggCO.next()) {
            aggTotalCo += aggCO.getAggregate('SUM', 'not_to_exceed_amount');
            var toGether = parseFloat(aggTotalCo) + parseFloat(current.parent_sow.total_sow_value);
            current.total_sow_value = toGether;
        }
    }
 
I am executing this scirpt on before BR with an order of 500. 
 
Any suggestions or comments to address it is highly encouraged.
 
Regards,
Imran
1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi @Imran1 

it's not really clear what you want to achieve, however, please be aware of the nature of "before" Business Rules. They are executed before anything is written to the database, and thus your GlideAggregate might not determine the correct values.

A solution might be using a "after" BR in combination with a current.setWorkflow(false); to prevent another round on server-side.

Mail 

View solution in original post

1 REPLY 1

Maik Skoddow
Tera Patron
Tera Patron

Hi @Imran1 

it's not really clear what you want to achieve, however, please be aware of the nature of "before" Business Rules. They are executed before anything is written to the database, and thus your GlideAggregate might not determine the correct values.

A solution might be using a "after" BR in combination with a current.setWorkflow(false); to prevent another round on server-side.

Mail