+ Start a Discussion
GMASJGMASJ 

Before insert is not updating records

Hi, 

In below trigger its working logically only problem I am facing is its not updating the mached records 
  
Have highlighted the code in bold which is not working. 
trigger CaseTrigger on Case (Before Insert,Before Update){ 
   if(Trigger.isBefore){ 
       CaseTriggerUtils.processInsert(Trigger.new);
    }     
}


public class CaseTriggerUtils {
    
    public static void processInsert(list<Case> cseLst) {        
        processUpdate(cseLst);       
    }   
    
    public static void processUpdate(Map<id, Case> newMap, Map<id, Case> oldMap) { 
        processUpdate(newMap.values());
    }
    
    public static void processUpdate(list<Case> cseLst) {
        String FWSubject;
        String RWSubject;
        String CaseSubject;
        list<Case> caseLst = new list<Case>();
   
        for (Case c : cseLst) {       
            if(c.subject <> null){     
                // Remove FW and RW from case subject to get exact string
                if(c.subject.Contains('FW:')){
                    FWSubject = c.subject.replaceAll('FW:','');
                    System.debug('FQ Trim String :' + FWSubject);
                    CaseSubject = FWSubject;     
                } else if(c.subject.Contains('RW:')){ 
                    RWSubject = c.subject.replaceAll('RW:','');
                    System.debug('RW Trim String :' + RWSubject);   
                    CaseSubject = RWSubject;
                }  else {
                    CaseSubject = c.subject;
                }  
            }                
        }
        
        // Query to get list of all case subject values
        list<case> ctendays = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = LAST_N_DAYS:10 and  subject like :('%' + CaseSubject + '%') order by createddate desc limit 100];
      
        for(case ctndays : ctendays){
            system.debug('Existing Cases :'  + ctndays.casenumber + ' ' + ctndays.subject + ' ' + ctndays.createddate + ' ' + ctndays.owner.name);      
            system.debug('Latest Case Owner ' + ctendays[0].owner.name); 
            
            // If case subject is more than 18 character
            if(ctndays.subject != null &&  ctndays.subject.length() > 18){
                if(FWSubject <> null){
                    ctndays.subject = FWSubject; 
                } else if (RWSubject <> null){
                    ctndays.subject = RWSubject; 
                }
            } 
 
                // Get the latest owner to assign ownership. 
               ctndays.ownerid = ctendays[0].ownerid;
               ctndays.Duplicate_With__c = ctendays[0].casenumber;
               ctndays.New_Hire_Email__c = ctendays[0].owner.name;
               //caseLst.add(ctndays);
            
        }  
  
    }
    
}

 
Raj VakatiRaj Vakati
Use this code
 
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
    if(firstRun){
      firstRun = false;
      return true;
    }else{
        return firstRun;
    }
    }
}
 
trigger CaseTrigger on Case (Before Insert,Before Update){ 
   if(Trigger.isBefore){ 
    if(AvoidRecursion.isFirstRun())
    {
       CaseTriggerUtils.processInsert(Trigger.new);
    }     
   }
}
 
public class CaseTriggerUtils {
    
    public static void processInsert(list<Case> cseLst) {        
        processUpdate(cseLst);       
    }   
    
    public static void processUpdate(Map<id, Case> newMap, Map<id, Case> oldMap) { 
        processUpdate(newMap.values());
    }
    
    public static void processUpdate(list<Case> cseLst) {
        String FWSubject;
        String RWSubject;
        String CaseSubject;
        list<Case> caseLst = new list<Case>();
   
        for (Case c : cseLst) {       
            if(c.subject <> null){     
                // Remove FW and RW from case subject to get exact string
                if(c.subject.Contains('FW:')){
                    FWSubject = c.subject.replaceAll('FW:','');
                    System.debug('FQ Trim String :' + FWSubject);
                    CaseSubject = FWSubject;     
                } else if(c.subject.Contains('RW:')){ 
                    RWSubject = c.subject.replaceAll('RW:','');
                    System.debug('RW Trim String :' + RWSubject);   
                    CaseSubject = RWSubject;
                }  else {
                    CaseSubject = c.subject;
                }  
            }                
        }
        
        // Query to get list of all case subject values
        list<case> ctendays = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = LAST_N_DAYS:10 and  subject like :('%' + CaseSubject + '%') order by createddate desc limit 100];
      
        for(case ctndays : ctendays){
            system.debug('Existing Cases :'  + ctndays.casenumber + ' ' + ctndays.subject + ' ' + ctndays.createddate + ' ' + ctndays.owner.name);      
            system.debug('Latest Case Owner ' + ctendays[0].owner.name); 
            
            // If case subject is more than 18 character
            if(ctndays.subject != null &&  ctndays.subject.length() > 18){
                if(FWSubject <> null){
                    ctndays.subject = FWSubject; 
                } else if (RWSubject <> null){
                    ctndays.subject = RWSubject; 
                }
            } 
 
                // Get the latest owner to assign ownership. 
              ctndays.ownerid = ctendays[0].ownerid;
               ctndays.Duplicate_With__c = ctendays[0].casenumber;
               ctndays.New_Hire_Email__c = ctendays[0].owner.name;
			   update ctndays ; 
               //caseLst.add(ctndays);
            
        }  
  
    }
    
}