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

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

This works! Thank you, thank you, thank you Abhijit!