+ Start a Discussion
GMASJGMASJ 

Convert cose block into trigger code

Hi,

   I want to add below code inside trigger please help should this is before insert/update or after insert/update 

  
String FWSubject;
  String RWSubject;
  String CaseSubject;
  list<case> cselst = new list<case>();

    for (Case c : [select id,casenumber,subject,ownerid,owner.name from case where id = '5002D000004JCQm']) {
       system.debug('Case Number :' + c.casenumber); 
       system.debug('Case Subject : ' + c.subject);
       system.debug('Case Subject length : ' + c.subject.length());
       system.debug('Case Owner Name : ' + c.owner.name);
       
       CaseSubject = c.subject;
       
       //Search for FW and RW key word were length of the subject is more than 18
        if(c.subject != null && c.subject.length() > 18){
            if(!c.subject.Contains('FW:')){
              FWSubject = c.subject.replaceAll('FW:','');
              System.debug('FQ Trim String :' + FWSubject);     
         }  
    
        if(!c.subject.Contains('RW:')){ 
         RWSubject = c.subject.replaceAll('RW:','');
         System.debug('RW Trim String :' + RWSubject);   
         }     
       }  
     }
     
   //Map<Id, case> c = new Map<Id, case>([select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10]);
     
   //Search for same case based on subject within 10days. 
   list<case> ctendays = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10];
    
   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); 
       
      ctndays.ownerid = ctendays[0].ownerid;
      ctndays.Duplicate_With__c = ctendays[0].casenumber;
       cselst.add(ctndays);
      }  
   
if(cselst.size()>0){
     update cselst;
}

Thanks
GMASJ
Malhar Patel PatelMalhar Patel Patel
I'd love to help, just need more info on what you're trying to achieve, or what do you think this piece of code is doing, some explanation.
GMASJGMASJ
Hi Malhar Patel,

     Log is I want to create a trigger on case object which will update its similar records based on the condition only thing is it has to update same object 

 I have put the logic below inside the trigger it is giving error. 
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTrigger caused an unexpected exception, contact your administrator: CaseTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5002D000004JCQhQAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CaseTrigger: maximum trigger depth exceeded Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate: []: Trigger.CaseTrigger: line 40, column 1
 * = Required InformationCase Information

Trigger 
trigger CaseTrigger on Case (after Insert, after Update){
  String FWSubject;
  String RWSubject;
  String CaseSubject;
  public static boolean isExecuted = True;
  list<Case> caseList=new list<Case>();
  
    for (Case c : trigger.new){
       CaseSubject = c.subject;
       //Search for FW and RW key word were length of the subject is more than 18
        if(c.subject != null && c.subject.length() > 18){
            if(!c.subject.Contains('FW:')){
              FWSubject = c.subject.replaceAll('FW:','');
              System.debug('FQ Trim String :' + FWSubject);     
            }  
    
          if(!c.subject.Contains('RW:')){ 
            RWSubject = c.subject.replaceAll('RW:','');
           System.debug('RW Trim String :' + RWSubject);   
         }     
       }  
    }
   
   if(isExecuted) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {
          isExecuted=False;
      //Map<Id, case> c = new Map<Id, case>([select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10]);
     
   //Search for same case based on subject within 10days. 
   list<case> ctendays = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10];
    
   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); 
       
      ctndays.ownerid = ctendays[0].ownerid;
      ctndays.Duplicate_With__c = ctendays[0].casenumber;
        caseList.add(ctndays);
      }  
       update caseList;
   
      }
    
    

 /*  if(Trigger.isBefore) {
      CaseTriggerUtils.CaseUtils(Trigger.new);     
   } */
   
    
      

}


Thanks 

Sudhir