function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pixelpixel 

Loop to sum value of a decimal field in an sObject

I have an sObject with a decimal field that I'm having problems creating an APEX loop that will sum the values contained in the instances of that field. The code for it is nested inside another loop. Below is some psuedo code for what's relevent.

 

Initially, before any of the loops, I created a List called Brn_Hrs. Beginning at (3) below, you'll see there's a query done to create a Temp list of records. After that, I set Brn_Hrs[0].Hours__c to (0.00) because I only want a total or sum as output from the loop. I also do not want to fill each element of the array as it goes through each iteration of the loop; both this one and the larger one it's nested inside of.

 

Finally we come to the new for loop I'm having difficulty with when we reach (4). 

 

List<Time__c> Brn_Hrs;
// (3) Create Temp list of Hours records called "BrnH_Tmp" List<Time__c> BrnH_Tmp = [SELECT Hours__c FROM Time__c WHERE Time__c.Resource__c = :rTime.Resource__c AND Time__c.OppLnk__c = :rTime.OppLnk__c]; // Below need to be reset to zero for each iteration of larger loop Brn_Hrs[0].Hours__c = (0.00); count = 0; // (4) Sum Total Burnt Hours for this combo do { Brn_Hrs[0].Hours__c += BrnH_Tmp.Hours__c ; }while (count < BrnH_Tmp.size);

 

As I understand it, I can't do the "+=" operation between a Decimal Variable and BrnH_Tmp because the latter is an sObject; thus the reason I specified the Brn_Hrs[0].Hours__c. Should I have instead specified Brn_Hrs[0].Hours__c[0] in order to make this approach work or do I need to use a "get" operation of some kind on BrnH_Tmp to sum all the instances of Hours__c in the BrnH_Tmp, perhaps by putting them into another variable? Or perhaps do I need to do something like use the "VALUE" operator on the sObject instead?

 

I'm still trying to learn how to use APEX's implicit FOR and DO-WHILE loops as opposed to the "for(i=0; i<j; i++)" type of syntax where I can also put "i" into the sObject's array name inside of brackets like what can be done with a specific value. I'm finding that quite frustrating in situations like this.

 

Any help to point me in the right direction would be most appreciated. :)

 

 - pixel

 

Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman

HI pixel,

 

Please try these lines of Apex code script for addition 

 

List<Time__c> Brn_Hrs;
// (3) Create Temp list of Hours records called "BrnH_Tmp"
List<Time__c> BrnH_Tmp = [SELECT Hours__c
FROM Time__c
WHERE Time__c.Resource__c = :rTime.Resource__c
AND Time__c.OppLnk__c = :rTime.OppLnk__c];
 
// Below need to be reset to zero for each iteration of larger loop
Brn_Hrs[0].Hours__c = (0.00);
//(4) Sum Total Burnt Hours for this combo
decimal tempSum = 0.00;
for(Time__c time:BrnH_Tmp){
tempSum += time.Hours__c ;
}
Brn_Hrs[0].Hours__c = tempSum;
I believe It will work for your case,If it works please mark as solved to help other for this particualr solution.
--
Regards
Usman

All Answers

MoUsmanMoUsman

HI pixel,

 

Please try these lines of Apex code script for addition 

 

List<Time__c> Brn_Hrs;
// (3) Create Temp list of Hours records called "BrnH_Tmp"
List<Time__c> BrnH_Tmp = [SELECT Hours__c
FROM Time__c
WHERE Time__c.Resource__c = :rTime.Resource__c
AND Time__c.OppLnk__c = :rTime.OppLnk__c];
 
// Below need to be reset to zero for each iteration of larger loop
Brn_Hrs[0].Hours__c = (0.00);
//(4) Sum Total Burnt Hours for this combo
decimal tempSum = 0.00;
for(Time__c time:BrnH_Tmp){
tempSum += time.Hours__c ;
}
Brn_Hrs[0].Hours__c = tempSum;
I believe It will work for your case,If it works please mark as solved to help other for this particualr solution.
--
Regards
Usman
This was selected as the best answer
pixelpixel

Thank you Mo, that solved my problem.

 

I now have another one in that I need to apply that result to a different sObject. Am trying to sort that out and will repost if I can't come up with a solution. I'm hoping if at the top of my code, I replace

 

List<Time__c>Brn_Hrs;

 

with

 

List<RescTrack__c>BrnHrs; (a different sObject)

 

That will facilitate the assignment of tempSum to the type of sObject I need it to ultimately go to (its also a decimal).

 

BTW, in the code below, for anyone who tries to use this solution, "Time" is a reserved variable, so it must be changed to something else.

 

Again, thanks for the help. :)

 

 - pixel