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
pooja chauchanpooja chauchan 

Calculations on a list

Hello everyone,

I am trying to do some data manipulation over records in a list. I was able to correctly build my list but now I am stuck with the calculations that I want to see happen.

Let's say I have record 1 and record 2 in my list. I want to grab a field called meter readings on both record and subtract the value of record 2 from the value from record 1 to get the actual energy use.

record 1 energy use = record 1 meter reading - record 2 meter reading.

And obvisouly I would like to repeat that for every records in my list except for the last one (since I would not have a second meter reading to do the calculation).

At first I thought of using a variable to control the for loop but I keep getting the following error message: "System.ListException: List index out of bounds: 15" 
Best Answer chosen by pooja chauchan
Gaurav NirwalGaurav Nirwal
List<Item__c> currentItemList = meterToItemMap.get(currentMeter);
if(currentItemList ! = null && currentItemList.size()>)
{
For( Integer i =0; i <currentItemList.size(); i++)
{
Integer x = i+1;
if(currentItemList.size()> x){
currentItemList[i].Item_Use__c =(currentItemList[i].Meter_readings__c - currentItemList[x].meter_readings__c)*currentItemList[i].Meter_Multiplier__c;
 }
 }

All Answers

Gaurav NirwalGaurav Nirwal
List<Item__c> currentItemList = meterToItemMap.get(currentMeter);
if(currentItemList ! = null && currentItemList.size()>)
{
For( Integer i =0; i <currentItemList.size(); i++)
{
Integer x = i+1;
if(currentItemList.size()> x){
currentItemList[i].Item_Use__c =(currentItemList[i].Meter_readings__c - currentItemList[x].meter_readings__c)*currentItemList[i].Meter_Multiplier__c;
 }
 }
This was selected as the best answer
Wizno @ ConfigeroWizno @ Configero
So as I understand it, the 1st record is the "initial" reading, and all subsequent records will be subtractions from the first? 
 
Meter__c[] readings = [SELECT Id, Reading__c, Reading_Date__c FROM Meter__c WHERE Account__c = 'Some_Assumed_Parent_Record' ORDER BY Reading_Date__c ASC];

Integer finalReading = readings[0].Reading__c; //Get initiall reading

for( Integer x =1; x<readings.size(); x++){ //Since we already for the first reading to set as the default, we can skip item 0
	finalReading -= readings[x].Reading__c;
}

/* 
	The above is a basic example, if you need to do multiple readings, it would look something like this
*/

public class MeterWrapper {
	Meter__c[] readings {get;set;}
	Integer finalReading {get;set;}

	public MeterWrapper(Meter__c[] readings){
		this.readings = readings;
	}

	public void calculateReading(){
		this.finalReading = readings[0].Reading__c; //Get initiall reading

		for( Integer x =1; x<readings.size(); x++){ //Since we already for the first reading to set as the default, we can skip item 0
			this.finalReading -= readings[x].Reading__c;
		}
	}
}

// The above lets you put the MeterWrappers in a map.. I.e.. Map<Id, MeterWrapper> Where the Id is an AccountId

I think this should cover a few scenarios for you, but keep in mind that the code is untested and may require some tweaking (especially since I used made up custom objects to fit the scenario).