2

Below I select all accounts and I remove accounts with the lastactivitydate <60 days. Now I have a problem with the offset size, because I show on each page 12 records, but If a record is removed, the page shows only 11 records.
How can I solve this problem?

TotalRecs = 0;
OffsetSize = 0;
LimitSize = 12;

 List <Account> Acc = [SELECT Id, OwnerId, Name, BillingCity, NumberOfEmployees, Owner.Name, LastActivityDate FROM Account ORDER BY Name limit ' + LimitSize + ' offset ' + OffSetSize];

   FOR(Integer j = 0; j < Acc.size(); j++) {
            IF(((today.getTime() - Acc.get(j).LastActivityDate.getTime())/(1000*60*60*24)) <60){
                Acc.remove(j);                              
            } 
        }
RETURN Acc;


EDIT 01.07.2015

Public List <Account> getAccList2() {
String SearchStringConvert = '%' + SearchString + '%';
Boolean isEmptySearch = String.isEmpty(SearchString);
Boolean isEmptyStatus = String.isEmpty(SelectedStatus);
Boolean isEmptyUser = String.isEmpty(SelectedUserId);
Boolean isEmptyStand = String.isEmpty(SelectedStand);    

IF (isEmptySearch && isEmptyStatus && isEmptyUser && isEmptyStand) {
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Please enter value'));
RETURN NULL;
}

String SelectClause = 'SELECT Id, OwnerId, Name, BillingCity, NumberOfEmployees, Owner.Name, (SELECT LastModifiedDate FROM Opportunities ORDER BY LastModifiedDate DESC Limit 1) FROM Account';
String LimitClause = ' ORDER BY Name limit ' + LimitSize + ' offset ' + OffSetSize;    
String CountSelectClause = 'SELECT Count() FROM Account';
String CountLimitClause = ' LIMIT 2000';

List <String> WhereClauses = new List <String>();

IF (!isEmptyStand) {
    IF (SelectedStand == 'GREEN') {
        WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:60)');
    } 
    ELSE IF (SelectedStand == 'YELLOW') {
        WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:60 AND LastModifiedDate = LAST_N_DAYS:180)');
    }
    ELSE IF (SelectedStand == 'RED') {
        WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:180)');
    }  
    ELSE IF (SelectedStand == 'LEER') {
        WhereClauses.Add('Id NOT IN (SELECT AccountId FROM Opportunity)');
    }          
}      

IF (!isEmptySearch) {
    WhereClauses.Add('Name LIKE \'' + SearchStringConvert + '\'');
}
IF (!isEmptyStatus) {
    WhereClauses.Add('Status__c = \'' + SelectedStatus + '\'');
}
IF (!isEmptyUser) {
    WhereClauses.Add('OwnerId = \'' + SelectedUserId + '\'');
}
String WhereClause = ' WHERE ' + String.join(WhereClauses, ' AND ');

String SQL = SelectClause + WhereClause + LimitClause;
String SQLCount = CountSelectClause + WhereClause + CountLimitClause;  

List <Account> Acc = Database.query(SQL);
TotalRecs = Database.CountQuery(SQLCount);

dateTime heute = dateTime.now();        

    FOR(Integer j = Acc.Size() - 1; j >=0; j--) {    
        IF (SelectedStand == 'YELLOW') {
            IF(((heute.getTime() - Acc.get(j).Opportunities[0].LastModifiedDate.getTime())/(1000*60*60*24)) <60){
                Acc.remove(j);                  
                TotalRecs--;                
            } 
        }
        ELSE IF (SelectedStand == 'RED') {
            IF(((heute.getTime() - Acc.get(j).Opportunities[0].LastModifiedDate.getTime())/(1000*60*60*24)) <180){
                Acc.remove(j);                 
                TotalRecs--;            
            } 
        }  
        }

RETURN Acc;

}
2

Just increment backwards through the list.

for(Integer j = Acc.size() - 1; j >= 0; j--) {
    if(Acc[j].LastActivityDate.date().daysBetween(Date.today()) < 60){
        Acc.remove(j);                              
    } 
}

However you can just do this in your SOQL without any post-processing.

List <Account> Acc = [SELECT Id FROM Account WHERE LastActivityDate < LAST_N_DAYS:60];

Edit

As for your updated filters, you can still accomplish what you want via SOQL. Merge in the following WHERE clauses to the appropriate evaluation section:

if (selectedStand == 'YELLOW')
{
    // some logic is already here
    whereClauses.add('LastModifiedDate < LAST_N_DAYS:60');
}
else if (selectedStand == 'RED')
{
    // some logic is already here
    whereClauses.add('LastModifiedDate < LAST_N_DAYS:180');
}
  • You can also get the days between two dates with Date.daysBetween, which is slightly less silly than doing four multiplications and a division (and uses less CPU time), but, like you say, prefer filtering when possible. – sfdcfox Jun 30 '15 at 20:09
  • Yeah I didn't even look very hard at that part of the evaluation. You're right though, if he goes that route it should be optimized. – Adrian Larson Jun 30 '15 at 20:11
  • for(Integer j = Acc.size() - 1; j >= 0; j--) surely. – Keith C Jun 30 '15 at 23:23
  • @KeithC So right. – Adrian Larson Jun 30 '15 at 23:24
  • 1
    @peXeq It's really hard when you keep changing your question. If you are just using lastModifiedDate instead of LastActivityDate, just change the query string to reflect that. – Adrian Larson Jul 2 '15 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.