How to calculate sum of values associated with a record.

Spike236
Tera Contributor

Hello folks,

I'm working on an exercise to create a business rule to get the reference of the challenge, probably with getRefRecord(), then find all of the contribution records associated with this challenge record to calculate the total sum of contribution records and then store the value in the challenge record, field 'Current status'.

I wanted to do it with the GlideAggregate but I'm really struggling with how to search for the records associated with the challenge and then store them. Btw,can't use calculated value

Hopefully this makes sense. Any help or suggestions are greatly appreciated 🙂

 

So far, I come up with this.

var contributionGA = new GlideAggregate ('x_866947_gvs_sport_contribution');
contributionGA.addAggregate('SUM','contribution');
contributionGA.query();

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

You are getting gliderecord in currentChallenge variable but you will have to dot walk to current status to store value as shown below, Inside if condition, you would need to write below line :

currentChallenge.current_status=contributionGA.getAggregate('SUM',"contribution"); //change field backend from current_status if needed
currentChallenge.update();

Also your 5th line should be as below,

contributionGA.addQuery("challenge",current.challenge);

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

6 REPLIES 6

Abhijit4
Mega Sage

You would just need to add filter for current record so that it wil pickup only those contributions. 

e.g. If you are planning to write BR on Challenge table then you can use below script

var contributionGA = new GlideAggregate ('x_866947_gvs_sport_contribution'); 
contributionGA.addQuery("challenge",current.sys_id);
contributionGA.groupBy("challenge");// your current record field name on contribution table
contributionGA.addAggregate('SUM','contribution');
contributionGA.query();
if(contributionGA.next()){
current.u_current_status=contributionGA.getAggregate('SUM',"contribution");
}

In case if you have glideobject in other variables, getting via getRefRecord method then replace current with that gliderecord variable.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hello Abhijit,

thank you very much for your response. However, this is somehow not working for me. Is it because that 'challenge" field is not on the Contribution table but the Challange table?

For the getRefRecord, would be something like this?

var currentChallenge = current.challenge.getRefRecord();

 

Thanks

Are you writing BR on challenge table?

if yes then you don't need to get challenge gliderecord again, it already exist with current object.

if no then please provide information like on which table you are writing BR? Please share BR scrrenshot which will inlcude table, condtion and script details so that I can help you further.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

I'm actually writing this on the Contribution table. Thank you for your help.

find_real_file.png