0

I have a custom apex page where the records will be displayed every month for a logged in user. They are as follows:

Before

Now, my requirement is to display only max records of an Account instead of all the records. It should be as follows:

after

Below is my controller:

public class PD_ClosedAccounts {
    public String accId {get;set;}
    public String accId2 {get;set;}
    public list<Custom_Month__c> accList2 {get; set;}
    public List<Custom_Month__c> CustomMonthList {get;set;}
    public list<string> dID = new list<string>();
    public map<Id,string> CusAccID = new map<Id,string>();
    public List<User> usr = new List<User>();

    public PD_ClosedAccounts(){
        // Get the logged in user's Account Id
         usr = [Select Id, PD_Acc__c From User Where Id=:UserInfo.getUserId()];

        if(usr != null ) {

            accId = usr[0].PD_Acc__c;

            Set<Id> allCusAccIDs = new Set<Id>();
            allCusAccIDs.add(accId);   


            // Get all the associated Acc records of the logged in user:
            for(Account associatedAcc: [Select ID, Cus_Acc_ID  From Account Where Id = :accId] ){
                CusAccID.put(associatedAcc.ID, associatedAcc.Cus_Acc_ID);
            }


           dID=CusAccID.values();

            CustomMonthList = [SELECT Name, Acc__c, Acc__r.Name, Acc__r.Acc_Id_Format__c,   
                             Acc__r.Level__c, Acc__r.Location_Country__c, Processing_Month__c, 
                              Acc_c__c FROM CustomMonthList WHERE Acc_c__c != 0 AND
                              Processing_Date__c >= LAST_MONTH 
                              AND Acc__r.Spn_Id__c IN :dID order by Acc__r.Acc_Id_Format__c DESC];

        }
         AggregateResult[] groupedResults = [SELECT id id, 
                                            MAX(Acc_c__c) maxAccountC 
                                             FROM CustomMonthList WHERE ID IN:CustomMonthList 
                                               group by  Processing_Date__c, 
                                            Acc__r.Acc_Id_Format__c];

        List<Id> theIds = new List<Id>();

        for (AggregateResult result : groupedResults) {
            theIds.add((Id) result.get('id'));
        }
        accList2 = [SELECT Acc__c, Acc__r.Acc_Id_Format__c  FROM Custom_Month__c WHERE ID IN :theIds];
        if(accList2!= Null){
            for(integer i=0;i<accList2.size();i++){
         accId2 =  accList2[i].Acc__c;
            } 
        }
    }
}

From the above controller, even though I have aggreagte query fetching max of Account_c__c field from CustomMonthList, I am getting all the 4 records in my page instead of Max ones.

Can anyone suggest the changes in my code so that I can able to fetch only max records?

  • Please use edit and define your schema. Specifically, what is CustomMonthList as AFAIK, there is no such OOTB Sobject. N.B. usr can never be null as UserInfo.getUserId() will always return a value – cropredy Oct 17 at 21:23

Your Answer

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

Browse other questions tagged or ask your own question.