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
SFDC Lightning 18SFDC Lightning 18 

Trigger not fetching the EHR_TS__c values

Hi Team,

Trigger not fetching the values all, i am inserting new records through data import wizard, i am not getting any error but after insert some records are updating and some records are not update, Example today inserted 500 records though data import wizard now its inserting 500 records but out of 500, 200 records its updating the EHR_TS__c value and remaining 300 not updating, and after i exported 300 records and update only ID, at that time 300 records its updating.

Please Help me where i am mistake in code its very urgent, below is my code Trigger & Class ,Thanks Advanced.

************************************ TRIGGER  ***********************************************************

Trigger UpdateEHRate on Time_Sheet__c (before insert,before update,after update, after insert)
{        
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)){   
        if(checkRecursive.isFirstRun()){

            List<Time_Sheet__c> timeList = EHRTriggerHandler.UpdateEHRate(trigger.New);
            if(timeList.size() > 0){
                update timeList;
            }
        }
    }
}

**************************************** APEX CLASS ****************************************************

public class EHRTriggerHandler{
    
    public static List<Time_Sheet__c> UpdateEHRate(List<Time_Sheet__c> lstTS){ 
       List<Time_Sheet__c> timeList = new List<Time_Sheet__c>();
        System.debug('@@@@@@@1'+timeList);
       set<Id> TSIds = new set<Id>();
       Map<Id ,boolean> mapTimeSheet = new Map<Id,boolean>();
        for(Time_Sheet__c EHR : lstTS) {    
            TSIds.add(EHR.Employee__c);
        }                       
        
        List<Estimated_Hourly_Rate__c> EHRlist = [SELECT Id, Name,Employee__c,Start_Date__c,End_Date__c,Estimated_Hourly_Rate_Variable__c 
                                              FROM Estimated_Hourly_Rate__c 
                                              WHERE Employee__c =:TSIds];  
        
        Map<id,List<Estimated_Hourly_Rate__c>> mapEs = new Map<id,List<Estimated_Hourly_Rate__c>>();
        
        for (Estimated_Hourly_Rate__c c : EHRlist) {
                List<Estimated_Hourly_Rate__c> l = mapEs.get(c.Employee__c);
                if (l == null) {
                    l = new List<Estimated_Hourly_Rate__c>();
                    mapEs.put(c.Employee__c, l);
                }
            l.add(c);
        }
        
        boolean isMatchFound = false;        
        //if(EHRlist!=null && !EHRlist.isEmpty()) {
         for(Time_Sheet__c timeSheetRec :lstTS){
             Time_Sheet__c timee = new Time_Sheet__c();  
             
             List<Estimated_Hourly_Rate__c> listes = mapEs.get(timeSheetRec.Employee__c);
            for(Estimated_Hourly_Rate__c EHR : listes) {   
                     if(timeSheetRec.Record_Date__c >= EHR.Start_Date__c && timeSheetRec.Record_Date__c <= EHR.End_Date__c ){                    
                        timee.id = timeSheetRec.id;
                        timee.EHR_TS__c = EHR.Estimated_Hourly_Rate_Variable__c; 
                        //timeSheetRec.EHR_TS__c = EHR.Estimated_Hourly_Rate_Variable__c;  
                        isMatchFound = true;
                        
                    } 
                }
                
                 timeList.add(timee);              
            }  
          //}             
           
        if(!isMatchFound){
            lstTS[0].addError('The given Record date does not fall under any of the Estimated Hourly Rates record(s)');
        }
        
        return timeList;
  }  
}
Best Answer chosen by SFDC Lightning 18
v varaprasadv varaprasad
Hi,

Please change your code like below and check once.
 
Map<id,List<Estimated_Hourly_Rate__c>> mapEs = new Map<id,List<Estimated_Hourly_Rate__c>>();
        

		
		 for (Estimated_Hourly_Rate__c c : EHRlist) {
		    if(mapEs.containsKey(c.Employee__c)){
			   List<Estimated_Hourly_Rate__c> l = mapEs.get(c.Employee__c);
			   l.add(c);
			   mapEs.put(c.Employee__c,l);
			}else{
			    List<Estimated_Hourly_Rate__c> l = new   List<Estimated_Hourly_Rate__c>();
				l.add(c);
			    mapEs.put(c.Employee__c,l);		
			}
		 }
		 
		 system.debug('===mapEs==='+mapEs);



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 

All Answers

v varaprasadv varaprasad
Hi,

Please change your code like below and check once.
 
Map<id,List<Estimated_Hourly_Rate__c>> mapEs = new Map<id,List<Estimated_Hourly_Rate__c>>();
        

		
		 for (Estimated_Hourly_Rate__c c : EHRlist) {
		    if(mapEs.containsKey(c.Employee__c)){
			   List<Estimated_Hourly_Rate__c> l = mapEs.get(c.Employee__c);
			   l.add(c);
			   mapEs.put(c.Employee__c,l);
			}else{
			    List<Estimated_Hourly_Rate__c> l = new   List<Estimated_Hourly_Rate__c>();
				l.add(c);
			    mapEs.put(c.Employee__c,l);		
			}
		 }
		 
		 system.debug('===mapEs==='+mapEs);



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
This was selected as the best answer
SFDC Lightning 18SFDC Lightning 18
HI v varaprasad,

I tried this code, But in production last week they inserted 543 new records and 200 records success and remaining 343 records its failed, and again they inserted 343 records now 200 success and 143 records failed. and third time 143 insert records are not updated.

Please check the code below.

************************************ TRIGGER  ***********************************************************

Trigger UpdateEHRate on Time_Sheet__c (before insert,before update,after update, after insert)
{        

try {
         
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)){   
        if(checkRecursive.isFirstRun()){

            List<Time_Sheet__c> timeList = EHRTriggerHandler.UpdateEHRate(trigger.New);
            if(timeList.size() > 0){
                //update timeList;
                Database.update(timeList, false);
            }
        } 
    }
}
catch(DmlException de) {
System.debug('--->The following DMLexception has occurred: ' + de.getMessage());
Integer numErrors = de.getNumDml();
    System.debug('---->getNumDml=' + numErrors);
    for(Integer i=0;i<numErrors;i++) 
    {
        System.debug('--->getDmlFieldNames=' + de.getDmlFieldNames(i));
        System.debug('--->getDmlMessage=' + de.getDmlMessage(i));  
    }
}
catch(Exception e) {
System.debug('--->The following exception has occurred: ' + e.getMessage());
}
}

**************************************** APEX CLASS ****************************************************

public class EHRTriggerHandler{
    
    public static List<Time_Sheet__c> UpdateEHRate(List<Time_Sheet__c> lstTS){ 
       List<Time_Sheet__c> timeList = new List<Time_Sheet__c>();
        System.debug('@@@@@@@1'+timeList);
       set<Id> TSIds = new set<Id>();
       Map<Id ,boolean> mapTimeSheet = new Map<Id,boolean>();
        for(Time_Sheet__c EHR : lstTS) {    
            TSIds.add(EHR.Employee__c);
        }                       
        
       List<Estimated_Hourly_Rate__c> EHRlist = [SELECT Id, Name,Employee__c,Start_Date__c,End_Date__c,Estimated_Hourly_Rate_Variable__c 
                                              FROM Estimated_Hourly_Rate__c 
                                              WHERE Employee__c =:TSIds];  
        
/* 
       Map<id,List<Estimated_Hourly_Rate__c>> mapEs = new Map<id,List<Estimated_Hourly_Rate__c>>();
        
        for (Estimated_Hourly_Rate__c c : EHRlist) {
                List<Estimated_Hourly_Rate__c> l = mapEs.get(c.Employee__c);
                if (l == null) {
                    l = new List<Estimated_Hourly_Rate__c>();
                    mapEs.put(c.Employee__c, l);
                }
            l.add(c);
        }
*/


Map<id,List<Estimated_Hourly_Rate__c>> mapEs = new Map<id,List<Estimated_Hourly_Rate__c>>();

for (Estimated_Hourly_Rate__c c : EHRlist) {
        if(mapEs.containsKey(c.Employee__c)){
              List<Estimated_Hourly_Rate__c> l = mapEs.get(c.Employee__c);
              l.add(c); mapEs.put(c.Employee__c,l);
     }
       else
     {
            List<Estimated_Hourly_Rate__c> l = new List<Estimated_Hourly_Rate__c>();
                l.add(c);
                mapEs.put(c.Employee__c,l);
      }
}
system.debug('===mapEs==='+mapEs);
        
        boolean isMatchFound = false;        
        //if(EHRlist!=null && !EHRlist.isEmpty()) {
         for(Time_Sheet__c timeSheetRec :lstTS){
             Time_Sheet__c timee = new Time_Sheet__c();  
             
             List<Estimated_Hourly_Rate__c> listes = mapEs.get(timeSheetRec.Employee__c);
            for(Estimated_Hourly_Rate__c EHR : listes) {   
                     if(timeSheetRec.Record_Date__c >= EHR.Start_Date__c && timeSheetRec.Record_Date__c <= EHR.End_Date__c ){                    
                        timee.id = timeSheetRec.id;
                        timee.EHR_TS__c = EHR.Estimated_Hourly_Rate_Variable__c; 
                        //timeSheetRec.EHR_TS__c = EHR.Estimated_Hourly_Rate_Variable__c;  
                        isMatchFound = true;
                        
                    } 
                }
                
                 timeList.add(timee);              
            }  
          //}             
           
        if(!isMatchFound){
            lstTS[0].addError('The given Record date does not fall under any of the Estimated Hourly Rates record(s)');
        }
        
        return timeList;
  }  
}