When I run the below code, I get this error:
System.ListException: List index out of bounds
Why?
public with Sharing class EngageDeltaController {
public static Engage__c[] getDelta() {
Engage__c[] x = [SELECT Name, ID,Score__c,CreatedDate,Account_ID__c,Delta__c FROM Engage__c ];
if(!x.isEmpty()){
for(Integer i=0,j=0; i<= x.size();i++)
{
if(x[i].Account_ID__c == x[i+1].Account_ID__c)
{
x[i].Delta__c = x[i+1].Score__c-x[i].Score__c;
j++;
}
else system.debug('nothing found');
}
}
return x;
}
}