function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce2015Salesforce2015 

Need modification in my class code

Hi Experts,
 
In our salesforce application, we are using below objects.

Objects and Fields

Below are case records.

Case Records

Perfect – If project having single case record, Total_Recurred_Hours__c showing correct value like 110 (i.e. 40+20+50).
Wrong – If project having two or more than two records, Total_Recurred_Hours__c showing wrong value like 160 (i.e. previous case record 110 value + 20 + 30). But it should be showing 20+30= 50 for second case record.

Below are my case record images

Case Record_1 for Rest Projet:

Case Record_1

Case Record_2 for Rest Projet:

Case Record_2

Anyone sort out my issue, Thanks in advance.
 
Thanks,
Manu
ManojjenaManojjena
Hi Manu ,

Please post your class code .
Prajakta JagtapPrajakta Jagtap
Hi Manu,
How are you calculating 'Total Recurred Hours'  on case record? by code or by using Salesforce OOB rollup summaries?
Salesforce2015Salesforce2015
Hi Prajakta,

Using Apex Class i'm updating the field value.

Thanks,
Manu.
Salesforce2015Salesforce2015
Hi Manoj,

Below is my peace of Apex Code...


 //Update the resource incurred hours
      list<MPM4_BASE__Milestone1_Time__c> times= new list<MPM4_BASE__Milestone1_Time__c>();
      times = [select id,MPM4_BASE__Incurred_By__c, MPM4_BASE__Hours__c from MPM4_BASE__Milestone1_Time__c  where Time_Card_Number__c =: pt.id limit 10000];
      Id incurredById = times[0].MPM4_BASE__Incurred_By__c;
      decimal totalHoursThisWeek=0;
      //add all candidate tasks that are going to need their counters updated to the set
      for(MPM4_BASE__Milestone1_Time__c aTime : times)
      {
          totalHoursThisWeek = totalHoursThisWeek + aTime.MPM4_BASE__Hours__c;
      }
      
      for(candidate_task__c candidateTask : lstCandidateTask)
      {
              if(candidateTask.Resource_incurred_hours__c == null)
                candidateTask.Resource_incurred_hours__c = 0;  
        
        system.debug('candidateTask.employee__r.id:: '+candidateTask.employee__r.id+' incurredById:: '+incurredById);
        if(candidateTask.employee__r.id == incurredById) {
          candidateTask.Resource_incurred_hours__c = candidateTask.Resource_incurred_hours__c + totalHoursThisWeek;
        } 
      }
      system.debug('before update lstCandidateTask:: '+lstCandidateTask);
      update lstCandidateTask;
      system.debug('after update lstCandidateTask:: '+lstCandidateTask);

      case caseObj;
        Id caseId;
      
      system.debug('lstCandidateTask:: '+lstCandidateTask);                        
      if(lstCandidateTask!= null && lstCandidateTask.size() > 0)
      {
        caseId = lstCandidateTask[0].case__r.id;
        caseObj = [select Id, total_recurred_hours__c from case where ID = : caseId];
        system.debug('existing caseObj:: '+caseObj );
        caseObj.total_recurred_hours__c = 0;
      }                                
  
        decimal totalRecurredHours = 0;
      for(candidate_task__c candidateTask : lstCandidateTask)
      {
              if(candidateTask.Resource_incurred_hours__c == null)
                candidateTask.Resource_incurred_hours__c = 0;  

        totalRecurredHours = totalRecurredHours + candidateTask.Resource_incurred_hours__c;
      }

      if(caseObj != null)
             caseObj.total_recurred_hours__c  = caseObj.total_recurred_hours__c+ totalRecurredHours;

      system.debug('lstCandidateTask:: '+lstCandidateTask);
      upsert caseObj;
             
            //WeeklyTimeCards__c WTC = [select id, name from WeeklyTimeCards__c where id =: lstTDel[0].Time_Card_Number__c limit 1];
        } catch(Exception ex){
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));  
        }    
        return null;
    }  
    public pageReference doRecall() {
        try {    
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(pt.id);  
            Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
            system.debug('---> pt.id '+ pt.id);
            //List<ProcessInstance> procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Pending' and TargetObjectId = :pt.id]);
            //Process Instance should be recalled even when the status is rejected 
            List<ProcessInstance> procins;
            procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Pending' and TargetObjectId = :pt.id]);

            if(procins == null)
                procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Rejected' and TargetObjectId = :pt.id]);
            
            List<ProcessInstanceWorkitem>  workitem; 
            system.debug('procins:: '+procins+'procins[0].id:: '+procins[0].id);
            if(procins != null && procins.size() > 0)
                workitem = new List<ProcessInstanceWorkitem>([select Id from ProcessInstanceWorkitem where ProcessInstanceId = :procins[0].id]);
            
            if ((workitem != null) && (workitem.size() > 0)) {
                pwr.SetComments('Recalled');
                pwr.setWorkItemId(workitem[0].id);
                pwr.setAction('Removed');
            }
Srini NandhiSrini Nandhi

  if(caseObj != null)
{            caseObj.total_recurred_hours__c=0;
             caseObj.total_recurred_hours__c  = caseObj.total_recurred_hours__c+ totalRecurredHours;
}


Can you replace and try? I am guessing caseObj.total_recurred_hours__c value adding to current one;
Prajakta JagtapPrajakta Jagtap
Hi Manu,
Kindly post debug statements for 
system.debug('lstCandidateTask:: '+lstCandidateTask);      &
system.debug('existing caseObj:: '+caseObj );

that means which are the records present in lstCandidateTask that will be used for looping.
Salesforce2015Salesforce2015
Hi Srini,

I tried your suggested code change, but no luck.
For better understanding of my code, please mail me on my mail manoharsfdc401@gmail.com
 
Prajakta JagtapPrajakta Jagtap
Please debug lstCandidateTask which is being used for summing up 'Resource Incurred Hours' of Candidate Tasks in for loop. That may hold invalid records or else please post here debug values for lstCandidateTask for more investigation.
Salesforce2015Salesforce2015
Hi Prajakta,

Please find below is my debuglog.

18.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
05:45:52.043 (43259772)|ENTERING_MANAGED_PKG|MPM4_BASE
05:45:52.069 (69776933)|SOQL_EXECUTE_BEGIN|[66]|Aggregations:0|SELECT Id, Name, MPM4_BASE__Project__c, MPM4_BASE__Parent_Milestone__c, MPM4_BASE__Complete__c, MPM4_BASE__Deadline__c, MPM4_BASE__Total_Actual_Hours__c, MPM4_BASE__Total_Estimated_Hours__c, MPM4_BASE__Total_Actual_Expense__c, MPM4_BASE__Total_Estimated_Expense__c, MPM4_BASE__Total_Hours_Budget__c, MPM4_BASE__Total_Expense_Budget__c, MPM4_BASE__Total_Complete_Tasks__c, MPM4_BASE__Total_Open_Tasks__c, MPM4_BASE__Total_Late_Tasks__c, MPM4_BASE__Total_Blocked_Tasks__c FROM Milestone1_Milestone__c WHERE Project__c = :tmpVar1
05:45:52.088 (88392638)|SOQL_EXECUTE_END|[66]|Rows:1
05:45:52.104 (104586857)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Milestone1_Project:a1u17000000Cmlh
05:45:52.104 (104599616)|VALIDATION_RULE|03dG00000010Oxf|Expense_Approval_Level1_Required
05:45:52.104 (104869242)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
  OR ( 
ISPICKVAL(Expense_Approval_Levels__c  , "Level1")
  , ISPICKVAL(Expense_Approval_Levels__c , "Level2")
  , ISPICKVAL(Expense_Approval_Levels__c  , "Level3")
),
ISBLANK(  Level1_Approver_Expense_Sheet__c)
)|Level1_Approver_Expense_Sheet__c=00517000000fZJf , Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.104 (104878469)|VALIDATION_PASS
05:45:52.104 (104881209)|VALIDATION_RULE|03dG00000010Oxg|Expense_Approval_Level2_NOTRequired
05:45:52.105 (105032149)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
  OR ( 
 ISPICKVAL(Expense_Approval_Levels__c , "Level1")
),
NOT(ISBLANK(  Level2_Approver_Expense_Sheet__c))
)|Level2_Approver_Expense_Sheet__c=null , Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.105 (105040200)|VALIDATION_PASS
05:45:52.105 (105042414)|VALIDATION_RULE|03dG00000010Oxh|Expense_Approval_Level2_Required
05:45:52.105 (105191738)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
  OR ( 
 ISPICKVAL(Expense_Approval_Levels__c , "Level2")
  , ISPICKVAL(Expense_Approval_Levels__c  , "Level3")
),
ISBLANK(  Level2_Approver_Expense_Sheet__c)
)|Level2_Approver_Expense_Sheet__c=null , Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.105 (105198870)|VALIDATION_PASS
05:45:52.105 (105200927)|VALIDATION_RULE|03dG00000010Oxi|Expense_Approval_Level3_NOTRequired
05:45:52.105 (105331431)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
  OR ( 
 ISPICKVAL(Expense_Approval_Levels__c , "Level2")
),
NOT(ISBLANK(  Level3_Approver_Expense_Sheet__c))
)|Level3_Approver_Expense_Sheet__c=null , Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.105 (105338407)|VALIDATION_PASS
05:45:52.105 (105340279)|VALIDATION_RULE|03dG00000010Oxj|Expense_Approval_Level3_Required
05:45:52.105 (105462676)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
  OR ( 
 ISPICKVAL(Expense_Approval_Levels__c  , "Level3")
),
ISBLANK(  Level3_Approver_Expense_Sheet__c)
)|Level3_Approver_Expense_Sheet__c=null , Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.105 (105469886)|VALIDATION_PASS
05:45:52.105 (105471783)|VALIDATION_RULE|03dG00000010Oxk|Expense_Approval_Levels_Required
05:45:52.105 (105568357)|VALIDATION_FORMULA|AND(Expense_Approval__c = true, 
ISBLANK(TEXT(Expense_Approval_Levels__c))
)|Expense_Approval__c=1 , Expense_Approval_Levels__c=Level1
05:45:52.105 (105574616)|VALIDATION_PASS
05:45:52.105 (105576496)|VALIDATION_RULE|03dG00000010OvD|Kickoff_before_deadline
05:45:52.105 (105752001)|VALIDATION_FORMULA|NOT(ISNULL(MPM4_BASE__Kickoff__c)) && NOT(ISNULL(MPM4_BASE__Deadline__c)) && MPM4_BASE__Deadline__c < MPM4_BASE__Kickoff__c|MPM4_BASE__Deadline__c=2015-12-31 00:00:00 , MPM4_BASE__Kickoff__c=2015-01-01 00:00:00
05:45:52.105 (105761720)|VALIDATION_PASS
05:45:52.105 (105765339)|VALIDATION_RULE|03dG00000010Oxl|Time_Approval_Level2_NotRequired
05:45:52.105 (105945336)|VALIDATION_FORMULA|AND(Time_Approval__c = true, 
  OR ( 
ISPICKVAL(Time_Approval_Levels__c  , "Level1")
),
NOT(ISBLANK(  Level2_Approver_Time_Sheet__c))
)|Time_Approval_Levels__c=Level1 , Time_Approval__c=1 , Level2_Approver_Time_Sheet__c=null
05:45:52.105 (105952982)|VALIDATION_PASS
05:45:52.105 (105955040)|VALIDATION_RULE|03dG00000010Oxm|Time_Approval_Level2_Required
05:45:52.106 (106178538)|VALIDATION_FORMULA|AND(Time_Approval__c = true, 
  OR ( 
    ISPICKVAL(Time_Approval_Levels__c , "Level2")
  , ISPICKVAL(Time_Approval_Levels__c  , "Level3")
),
ISBLANK(  Level2_Approver_Time_Sheet__c)
)|Time_Approval_Levels__c=Level1 , Time_Approval__c=1 , Level2_Approver_Time_Sheet__c=null
05:45:52.106 (106189363)|VALIDATION_PASS
05:45:52.106 (106192642)|VALIDATION_RULE|03dG00000010Oxn|Time_Approval_Level3_NotRequired
05:45:52.106 (106396499)|VALIDATION_FORMULA|AND(Time_Approval__c = true, 
  OR ( 
ISPICKVAL(Time_Approval_Levels__c  , "Level2")
),
NOT(ISBLANK(  Level3_Approver_Time_Sheet__c))
)|Time_Approval_Levels__c=Level1 , Time_Approval__c=1 , Level3_Approver_Time_Sheet__c=null
05:45:52.106 (106406431)|VALIDATION_PASS
05:45:52.106 (106409536)|VALIDATION_RULE|03dG00000010Oxo|Time_Approval_Level3_Required
05:45:52.106 (106613221)|VALIDATION_FORMULA|AND(Time_Approval__c = true, 
  OR ( 
     ISPICKVAL(Time_Approval_Levels__c  , "Level3")
),
ISBLANK(  Level3_Approver_Time_Sheet__c)
)|Time_Approval_Levels__c=Level1 , Time_Approval__c=1 , Level3_Approver_Time_Sheet__c=null
05:45:52.106 (106623302)|VALIDATION_PASS
05:45:52.106 (106626930)|VALIDATION_RULE|03dG00000010Oxp|Time_Approval_Levels_Required
05:45:52.106 (106785626)|VALIDATION_FORMULA|AND(Time_Approval__c = true, 
ISBLANK(TEXT(Time_Approval_Levels__c))
)|Time_Approval_Levels__c=Level1 , Time_Approval__c=1
05:45:52.106 (106794602)|VALIDATION_PASS
05:45:52.106 (106797950)|VALIDATION_RULE|03dG00000010OvE|Total_Expense_Budget_Negative_Numbers
05:45:52.106 (106908300)|VALIDATION_FORMULA|MPM4_BASE__Total_Expense_Budget__c  < 0|MPM4_BASE__Total_Expense_Budget__c=0.00
05:45:52.106 (106917157)|VALIDATION_PASS
05:45:52.106 (106920364)|VALIDATION_RULE|03dG00000010OvF|Total_Hours_Budget_Negative_Numbers
05:45:52.107 (107070357)|VALIDATION_FORMULA|MPM4_BASE__Total_Hours_Budget__c  < 0|MPM4_BASE__Total_Hours_Budget__c=0.00
05:45:52.107 (107079164)|VALIDATION_PASS
05:45:52.107 (107106851)|CODE_UNIT_FINISHED|Validation:Milestone1_Project:a1u17000000Cmlh
05:45:52.132 (132526295)|CODE_UNIT_STARTED|[EXTERNAL]|01qG0000000wCaZ|updateProjectApprovers on Milestone1_Project trigger event AfterUpdate for [a1u17000000Cmlh]
05:45:52.132 (132809523)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
05:45:52.132 (132843943)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
05:45:52.132 (132880669)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
05:45:52.132 (132897295)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
05:45:52.132 (132930802)|SYSTEM_METHOD_ENTRY|[4]|List<MPM4_BASE__Milestone1_Project__c>.iterator()
05:45:52.132 (132954787)|SYSTEM_METHOD_EXIT|[4]|List<MPM4_BASE__Milestone1_Project__c>.iterator()
05:45:52.132 (132972056)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
05:45:52.132 (132990517)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
05:45:52.133 (133044260)|SYSTEM_METHOD_ENTRY|[6]|Map<Id,MPM4_BASE__Milestone1_Project__c>.get(Object)
05:45:52.133 (133074769)|SYSTEM_METHOD_EXIT|[6]|Map<Id,MPM4_BASE__Milestone1_Project__c>.get(Object)
05:45:52.133 (133171788)|SYSTEM_METHOD_ENTRY|[7]|Id.compareTo(Id, Id, Boolean)
05:45:52.133 (133206114)|SYSTEM_METHOD_EXIT|[7]|Id.compareTo(Id, Id, Boolean)
05:45:52.133 (133327190)|SYSTEM_METHOD_ENTRY|[14]|Id.compareTo(Id, Id, Boolean)
05:45:52.133 (133352426)|SYSTEM_METHOD_EXIT|[14]|Id.compareTo(Id, Id, Boolean)
05:45:52.133 (133420458)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
05:45:52.133 (133433372)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
05:45:52.134 (134133114)|SOQL_EXECUTE_BEGIN|[22]|Aggregations:0|SELECT Id, Project_Id__c, Project_Id__r.Level1_Approver_Time_Sheet__c, Project_Id__r.Level2_Approver_Time_Sheet__c, Project_Id__r.Level3_Approver_Time_Sheet__c, Level1_Approver_Time_Sheet__c, Level2_Approver_Time_Sheet__c, Level3_Approver_Time_Sheet__c FROM WeeklyTimeCards__c WHERE Project_Id__c = :tmpVar1
05:45:52.137 (137778288)|SOQL_EXECUTE_END|[22]|Rows:0
05:45:52.137 (137973892)|SYSTEM_METHOD_ENTRY|[29]|List<WeeklyTimeCards__c>.iterator()
05:45:52.138 (138082065)|SYSTEM_METHOD_EXIT|[29]|List<WeeklyTimeCards__c>.iterator()
05:45:52.138 (138106931)|SYSTEM_METHOD_ENTRY|[29]|system.ListIterator.hasNext()
05:45:52.138 (138121464)|SYSTEM_METHOD_EXIT|[29]|system.ListIterator.hasNext()
05:45:52.158 (158465366)|SOQL_EXECUTE_BEGIN|[36]|Aggregations:0|SELECT Id, Project_Id__c, Project_Id__r.Level1_Approver_Expense_Sheet__c, Project_Id__r.Level2_Approver_Expense_Sheet__c, Project_Id__r.Level3_Approver_Expense_Sheet__c, Level1_Approver_Expense_Sheet__c, Level2_Approver_Expense_Sheet__c, Level3_Approver_Expense_Sheet__c FROM WeeklyExpenseCard__c WHERE Project_Id__c = :tmpVar1
05:45:52.161 (161622745)|SOQL_EXECUTE_END|[36]|Rows:0
05:45:52.161 (161882164)|SYSTEM_METHOD_ENTRY|[43]|List<WeeklyExpenseCard__c>.iterator()
05:45:52.162 (162020742)|SYSTEM_METHOD_EXIT|[43]|List<WeeklyExpenseCard__c>.iterator()
05:45:52.162 (162058654)|SYSTEM_METHOD_ENTRY|[43]|system.ListIterator.hasNext()
05:45:52.162 (162084777)|SYSTEM_METHOD_EXIT|[43]|system.ListIterator.hasNext()
05:45:52.701 (162298272)|CUMULATIVE_LIMIT_USAGE
05:45:52.701|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

05:45:52.701|LIMIT_USAGE_FOR_NS|MPM4_BASE|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

05:45:52.701|CUMULATIVE_LIMIT_USAGE_END

05:45:52.162 (162396096)|CODE_UNIT_FINISHED|updateProjectApprovers on Milestone1_Project trigger event AfterUpdate for [a1u17000000Cmlh]
Salesforce2015Salesforce2015
Hi Prajaktha,

total i got 8 debuglogs. I'm confused which one correct.
Please suggest me, where i need to put debuglog statement.
Salesforce2015Salesforce2015
Hi All,

for my above scenario, I tried with below trigger. Even trigger not fulfill my requirement.
Any help will uprishiated.

Trigger:


trigger RollUpIncurredHours on Candidate_Task__c ( after insert, after update,after delete,after undelete) {
     Set<Id> caseIdSet=new Set<Id>();
     List<Case> caseListToUpdate=new List<Case>();
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
        for(Candidate_Task__c cand : Trigger.new){
            if(cand.Case__c != null)
                caseIdSet.add(cand.Case__c);     
        }
    }If(Trigger.isDelete){
       for(Candidate_Task__c cand : Trigger.old){
            if(cand.Case__c != null)
                caseIdSet.add(cand.Case__c);     
        }
    }
   for(AggregateResult res : [SELECT Case__c,sum(Resource_incurred_hours__c)can FROM Candidate_Task__c WHERE Case__c IN :caseIdSet group by Case__c]) {
          caseListToUpdate.add(new Case(Id=(Id)res.get('Case__c'),Total_Recurred_Hours__c=(Double)res.get('can')));
    }
    try{
      update caseListToUpdate;
    }catch(DmlException de){
      System.debug(de);
    }
}

Thanks,
Manu
Prajakta JagtapPrajakta Jagtap
Hi Manu,
first of all you dont need to write any kind of apex code to achieve the functionality as you want to show addition of 'Resource Incurred Hours' of all 'Project-Task-Resources' records in 'Total Recurred Hours' field on parent case record. Just create a rollup summary custom field on case object which will do sum of 'Resource Incurred Hours' field of all child  'Project-Task-Resources' records. 
Salesforce2015Salesforce2015
Prajakta, you are correct.
But lookup relationship already there with no.of records.