1

I have a custom LWC page which will display the Date and other Fields. I'm iterating the date Based on my start and End date.

I'm trying to exclude the Saturday and Sunday from my loop my adding below Code.

 DateTime dt=DateTime.newInstance(startDate, Time.newInstance(0, 0, 0, 0));
            String dayOfWeek=dt.format('E');

            if (dayOfWeek  == 'Sat' || dayOfWeek  == 'Sun'){
                system.debug('Week End' + dayOfWeek);
                //system.debug('Other');
                //  break;
            } else{
                system.debug('Week Days');


By Adding the Above Code I'm getting Apex CPU time limit exceeded with 18Mb File Log, If i remove that code I'm getting the response with 170kb log.

When I tried to open the Console log to find the bottom neck of the Issue, My browser crashes. Please help any issue in my Code.

@AuraEnabled
   public static List<timeSheetWrapperNew> getTimeSheetDays(Id loggedinUserId, Id timesheetId) {


       //Timesheet
        emitCS__Time_Sheet__c  timesheet;
       if(  
           emitCS__Time_Sheet__c.SObjectType.getDescribe().isAccessible()  &&
           Schema.SObjectType.emitCS__Time_Sheet__c.fields.emitCS__End_Date__c.isAccessible() &&            
           Schema.SObjectType.emitCS__Time_Sheet__c.fields.emitCS__Start_Date__c.isAccessible())
       {            
           timesheet=   [SELECT  Id, emitCS__End_Date__c,emitCS__Start_Date__c FROM 
                         emitCS__Time_Sheet__c WHERE emitCS__Contact__c = :loggedinUserId and id=:timesheetId];
       }

       //Timesheet Days
       list<emitCS__Time_Sheet_Day__c> tsdays;
       if(  
           emitCS__Time_Sheet_Day__c.SObjectType.getDescribe().isAccessible()  &&
           Schema.SObjectType.emitCS__Time_Sheet_Day__c.fields.emitCS__Date__c.isAccessible() )
       {  

       tsdays =[SELECT Id, emitCS__Date__c, emitCS__Project__r.name   FROM emitCS__Time_Sheet_Day__c 
                WHERE emitCS__Time_Sheet__c=:timesheet.Id ];

       }

       //Timesheet Datails
       List<emitCS__Time_Sheet_Details__c> timeSheetDetails ;

       system.debug('tsdays' + tsdays);
           if(emitCS__Time_Sheet_Details__c.SObjectType.getDescribe().isAccessible()  &&
              Schema.SObjectType.emitCS__Time_Sheet_Details__c.fields.Id.isAccessible()  &&      
              Schema.SObjectType.emitCS__Time_Sheet_Details__c.fields.emitCS__Time_Sheet_Day__c.isAccessible()  &&            
              Schema.SObjectType.emitCS__Time_Sheet_Details__c.fields.emitCS__Type_of_Hours__c.isAccessible() &&
              Schema.SObjectType.emitCS__Time_Sheet_Details__c.fields.emitCS__Hours__c.isAccessible() 
             ){                 
                 timeSheetDetails = [SElect Id, emitCS__Time_Sheet_Day__c, emitCS__Type_of_Hours__c, emitCS__Hours__c 
                                     ,emitCS__Project__r.name from emitCS__Time_Sheet_Details__c 
                                     where emitCS__Time_Sheet_Day__c IN: tsdays ];           
             }


      /* Map<Id, List<emitCS__Time_Sheet_Details__c>> tDetailsByDayId= new Map<Id, List<emitCS__Time_Sheet_Details__c>>();
       for(emitCS__Time_Sheet_Details__c detail:timeSheetDetails){
           if(tDetailsByDayId.get(detail.emitCS__Time_Sheet_Day__c) == null){
               tDetailsByDayId.put(detail.emitCS__Time_Sheet_Day__c, new List<emitCS__Time_Sheet_Details__c>());
           }
           tDetailsByDayId.get(detail.emitCS__Time_Sheet_Day__c).add(detail);
       }*/ 

       List<timeSheetWrapperNew> tsdwrap = new List<timeSheetWrapperNew>();

       decimal index =0;
       decimal tsdetailIndex = 0;
       /*START*/
       Date startDate = timesheet.emitCS__Start_Date__c;
       Date endDate = timesheet.emitCS__End_Date__c;
       Id timeSheetDayrecId;
       /*Iterating the Date*/
       while (startDate < = endDate )
       {
           datetime startDatetime= Date.Valueof(startDate);  
           String strstartDay = startDatetime.formatGMT('dd');    
           String strstartDayYear = startDatetime.formatGMT('YYYY');  
           String strStartDatedisplay = startDatetime.formatGMT('MMM dd');
           string startdatesuffix = DateSuffix(integer.valueof(strstartDay));

          // string weekDays = startDatetime.formatGMT('E');
           //system.debug('startDatetime.format' + startDatetime.format('E'));
          //  if (startDatetime.format('E') != 'Sat' && startDatetime.format('E') != 'Sun') {

           DateTime dt=DateTime.newInstance(startDate, Time.newInstance(0, 0, 0, 0));
           String dayOfWeek=dt.format('E');

           if (dayOfWeek  == 'Sat' || dayOfWeek  == 'Sun'){
               system.debug('Week End' + dayOfWeek);
               //system.debug('Other');
               //  break;
           } else{
               system.debug('Week Days');


           timeSheetWrapperNew aWraper = new timeSheetWrapperNew() ; 
           //timeSheetDetailsWrapper tsdWraper = new timeSheetDetailsWrapper() ;
           List<timeSheetDetailsWrapper> tsdtlwrap = new List<timeSheetDetailsWrapper>();

           if (tsdays.size() > 0) {
               for(emitCS__Time_Sheet_Day__c days:tsdays){

                   if(days.Id !=null && days.emitCS__Date__c  == startDate){ 
                       timeSheetDayrecId = days.Id;
                       aWraper.timeSheetDateId = timeSheetDayrecId; 
                       aWraper.indexVal = index;
                       //Time Sheet Details;
                       system.debug('timeSheetDetails.size()' + timeSheetDetails.size());
                       if (timeSheetDetails.size() >0) {
                           for(emitCS__Time_Sheet_Details__c tsDetails:timeSheetDetails){
                               timeSheetDetailsWrapper tsdWraper = new timeSheetDetailsWrapper() ; 
                               if(timeSheetDayrecId == tsDetails.emitCS__Time_Sheet_Day__c){
                                   tsdWraper.projectName = tsDetails.emitCS__Project__r.name;
                                   tsdWraper.timeSheetDetailID =tsDetails.Id;
                                   tsdWraper.timeSheetHoursType =tsDetails.emitCS__Type_of_Hours__c;
                                   tsdWraper.timeSheetHours = tsDetails.emitCS__Hours__c;  
                                   tsdWraper.timesheetDetailIndex = tsdetailIndex;
                                   // system.debug('If_+++++++++++'  );
                                   tsdetailIndex = tsdetailIndex +1;
                                   tsdtlwrap.add(tsdWraper);
                               }
                           }
                       }else{
                           system.debug('Else'); 
                           timeSheetDetailsWrapper tsdWraper = new timeSheetDetailsWrapper() ;
                           tsdWraper.projectName = '';
                           tsdWraper.timeSheetDetailID =null;
                           tsdWraper.timeSheetHoursType ='Billable Regular';
                           tsdWraper.timeSheetHours = 0;
                           tsdWraper.timesheetDetailIndex = 0;
                           tsdtlwrap.add(tsdWraper);     
                       }

                       aWraper.timesheetDetails = tsdtlwrap;
                   }else{
                       timeSheetDayrecId =null;
                       aWraper.timeSheetDateId = timeSheetDayrecId;    
                       aWraper.indexVal = Index;
                       timeSheetDetailsWrapper tsdWraper = new timeSheetDetailsWrapper() ;
                       tsdWraper.projectName = '';
                       tsdWraper.timeSheetDetailID =null;
                       tsdWraper.timeSheetHoursType ='Billable Regular';
                       tsdWraper.timeSheetHours = 0;
                       tsdWraper.timesheetDetailIndex = 0;
                       tsdtlwrap.add(tsdWraper);                         
                   }     
                   aWraper.timesheetDetails = tsdtlwrap;
               }

           }else{
               timeSheetDetailsWrapper tsdWraper = new timeSheetDetailsWrapper() ;
               aWraper.timeSheetDateId = null;        
               aWraper.indexVal = Index;
               tsdWraper.projectName = '';
               tsdWraper.timeSheetDetailID =null;
               tsdWraper.timeSheetHoursType ='Billable Regular';
               tsdWraper.timeSheetHours = 0;
               tsdWraper.timesheetDetailIndex = 0;
               tsdtlwrap.add(tsdWraper);
               aWraper.timesheetDetails = tsdtlwrap;
           }


           //Formatting the Date to Display            
           //datetime startDatetime= Date.Valueof(StartDate);  


           aWraper.timeSheetDate =  strStartDatedisplay + startdatesuffix + ' ' ;   
           StartDate = StartDate +1;
           aWraper.timeSheetId = timesheet.Id;
           tsdwrap.add(aWraper);
           index = index +1;
            }
       }      

       return tsdwrap;     

   }

2

Issue Resolved, In the Below if Condition I have to add StartDate = StartDate +1;, Since I'm not adding this in My if condition It's causing an infinite loop.(it's My Bad).

  if (dayOfWeek  == 'Sat' || dayOfWeek  == 'Sun'){
               system.debug('Week End' + dayOfWeek);
              StartDate = StartDate +1;
           } else{
               system.debug('Week Days');

Finally my Debug log got Opened and found this issue.

| improve this answer | |

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.