1

I am working on one scenario. I have an object called "Time__c" Time__c has fields like "Role__c" , "Rate__c" "Hours__c", now my requirement is that I need to fetch all the records of time and show on VF Page but if Role__c is same and rate__c is same then I want those records to combine and have total of hours.

For example,
Record1 Role__c= Developer Rate__c=8 and Hours__c=8

Record2 Role__c=Admin Rate__c=6 and Hours__C=5

Record3 Role__c=Developer Rate__c=8 and Hours__c=7, then I should get only one record on VF Page for Developer role which should give 8+7=15hrs.

If Role names are same and are having different rates then I should show two developer roles on vf page Can anyone help me out how to achieve this.

1 Answer 1

-1

//This is Visualforce Page

<apex:page Controller="TimeTaskController" sidebar="false" showHeader="false">
  <apex:form >
      <apex:pageBlock title="Time Details">
          <apex:pageblockSection >
              <apex:pageBlockTable value="{!timeList}" var="t">
                  <apex:column value="{!t.Role__c}"/>
                  <apex:column value="{!t.Rate__c}"/>
                  <apex:column value="{!t.Hour__c}"/>

              </apex:pageBlockTable>
          </apex:pageblockSection>
          <apex:commandButton action="{!timeMergeRecords}" value="Merge"/>
      </apex:pageBlock>
  </apex:form>
</apex:page>

//------------------------------------------------------------------------------
// This is Controller.
public class TimeTaskController {
public List<Time__c> timeList{get;set;}
public List<Time__c> timeListAdmin;
public List<Time__c> timeListDeveloper;
Decimal adminHour;
Decimal devHour;
public TimeTaskController() {
    timeList = new List<Time__c>();
    timeListAdmin = new List<Time__c>();
    timeListDeveloper = new List<Time__c>();
}


public void timeMergeRecords(){
    adminHour=0;
    devHour=0;
    for(Integer i=0; i<=10 ; i++){
        timeListAdmin = [Select id , Name , Role__c , Hour__c From Time__c where Role__c='Admin' AND Rate__c=:i ];
        if(timeListAdmin.size()==1){
            timeList.add(new Time__c(Role__c='Admin', Rate__c =i , Hour__c=timeListAdmin[0].Hour__c ));
        }
        else if(timeListAdmin.size()>1){
            for(Time__c t : timeListAdmin){
                adminHour += t.hour__c;    
            }
            timeList.add(new Time__c(Role__c='Admin', Rate__c =i,Hour__c=adminHour ));
        }


        timeListDeveloper = [Select id , Name , Role__c , Hour__c From Time__c where Role__c='Developer' AND Rate__c=:i ];
        if(timeListDeveloper.size()==1){
            timeList.add(new Time__c(Role__c='Developer', Rate__c =i , Hour__c=timeListDeveloper[0].Hour__c ));
        }
        else if(timeListDeveloper.size()>1){
            for(Time__c t : timeListDeveloper){
                devHour += t.hour__c;    
            }
            timeList.add(new Time__c(Role__c='Developer', Rate__c =i,Hour__c=devHour ));  
        }   
    }
}

} // It's not best practice but i have done this task now you can also you map and done SOQl query without inside loop.

14
  • Hi @Venkata Sainath , I hope your problem will be solve , but it's not good approach to reach at the solution , and in this code is covering all conditions as you mentioned in your question, After this you can use to Map for best approach .Thank you. Commented Mar 6, 2019 at 5:34
  • Hello @Manish Mourya, Thanks a lot for quick reply and giving me the solution, I am new to Apex so just asked help. Regarding my question I just gave example of Admin and Developer but I can have multiple records with multiple Roles, if role name is matched and rate is matched then I want to group all those records and show on vf page with one record which should give sum of hours for matching roles and rates. Can you help me out in this if possible
    – user61203
    Commented Mar 6, 2019 at 6:59
  • Yes I understood your scenario , you just try above code , this will definitely work according your scenario. Thank You , If You got complete satisfaction about your problem then please tik this question as answered. Commented Mar 6, 2019 at 7:07
  • will it work for other roles, lets say if I have role like "Consultant"?bcoz in code we did not mentioned about consultant
    – user61203
    Commented Mar 6, 2019 at 7:08
  • But you mentioned , role only Developer and Admin so this code works only two role , if you want to add role consultant then , we have to write some code of line for it. Commented Mar 6, 2019 at 7:10

You must log in to answer this question.