0

This controller get the same data in two queries. The first (listGoalsCurrYear) gets all records for the current year, the second (listGoalsPrevYears) gets all in previous years. How to get all of the data in one query but to put the previous years in a second list with the same name so I don't need to redo my VF page?

public class vf_CommunityPlanGoalPSetting_Cntrlr {

    public List<Community_Plan_Goal_Setting__c> listGoalsCurrYear {get;set;}
    public List<Community_Plan_Goal_Setting__c> listGoalsPrevYears {get;set;}
    public String ft { get; set; }
    public String ccp { get; set; }
    public String fa { get; set; }
    public String fd { get; set; }
    public String ga { get; set; }
    public String gac { get; set; }    
    public String newRow {get;set;}

    public String currentYear = String.valueOf(System.Today().year()) ; 
    public String parentID;

    //Variable to hold add records to be deleted
    public List<Community_Plan_Goal_Setting__c> deleteList = new List<Community_Plan_Goal_Setting__c>();

    public vf_CommunityPlanGoalPSetting_Cntrlr(ApexPages.StandardController controller) 
    {    

        parentID = controller.getID();

        listGoalsCurrYear = [Select id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                             Core_Current_Participation__c,
                             Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c 
                             From Community_Plan_Goal_Setting__c 
                             Where Community_Plan__r.id = :parentID
                             AND Year__c = :currentYear
                             Order By Year__c];

        listGoalsPrevYears = [Select id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                              Core_Current_Participation__c,
                              Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c 
                              From Community_Plan_Goal_Setting__c 
                              Where Community_Plan__r.id = :parentID
                              AND Year__c <> :currentYear
                              Order By Year__c]; 
    }

    public pageReference AddRow()
    {
        // Create new record for each row
        Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c = String.valueOf(System.Today().year()), Community_Plan__c = parentID);

        //This next line allowes fields to be displayed in edit mode for new records
        if( cpgs.year__c == String.valueOf(System.Today().year()) ) newRow = 'nr';        

        listGoalsCurrYear.add(cpgs);

        return null;
    }

    public pageReference SaveRecords()
    {
        ApexPages.Message myMsgSuccess = new ApexPages.Message(ApexPages.Severity.INFO,'Success');  
        //Save the list of records
        upsert listGoalsCurrYear;   
        if(deleteList.size()>0)
        {
            delete deleteList;
        }

        // Show feedback
        ApexPages.addMessage(myMsgSuccess);

        return null;    
    }

    //Remove a record from the table.
    public pageReference removeRecord(){
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));

        //If existing then add it to the list to delete from the database
        if(listGoalsCurrYear[indexVal - 1].Id != null)
            deleteList.add(listGoalsCurrYear[indexVal - 1]);

        //Remove from the table    
        listGoalsCurrYear.remove(indexVal - 1);   

        return null;
    }    

}

2 Answers 2

0

query without using year and then using if condition to filter the record.

   listGoalsCurrYear = [Select id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                                 Core_Current_Participation__c,Year__c,
                                 Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c 
                                 From Community_Plan_Goal_Setting__c 
                                 Where Community_Plan__r.id = :parentID
                                 Order By Year__c];
for(Community_Plan_Goal_Setting__c cs : listGoalsCurrYear)
{
     if(cs.Year__c == String.valueOf(System.Today().year()) ) ) //if type is string otherwise use System.Today().year()
         currentYearList.add(cs);
     else
          lastYearList.add(cs);
}
1

Something like this:

Community_Plan_Goal_Setting__c[] listGoalsCurrYear  = new Community_Plan_Goal_Setting__c[]{};
    Community_Plan_Goal_Setting__c[] listGoalsPrevYears = new Community_Plan_Goal_Setting__c[]{};

    for(Community_Plan_Goal_Setting__c cpgs : [ SELECT /fields you need */
                                                FROM Community_Plan_Goal_Setting__c
                                                WHERE Community_Plan__r.id = :parentID
                                                ORDER BY Year__c]){
        if(cpgs.Year__c == currentYear){
            listGoalsCurrYear.add(cpgs);
        }
        else {
            listGoalsPrevYears.add(cpgs);
        }
    }
2
  • Very clear. I wish I could give credit to you as well but Tushar answered first. Thanks.
    – Apex N-u-b
    Aug 17, 2016 at 7:56
  • @ApexN-u-b you can upvote the answer to give some credit. Aug 17, 2016 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .