• sathish Tadikamalla 8
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hi All,

Here by i'm adding my trigger & triggerHandler class that i have written. Can anybody give me TestClass for this?Urgent reply needed.
Trigger
_------------------
trigger CaseTrigger on Case (after undelete, before delete, before insert, after update) {

    CaseTriggerHandler handler = new CaseTriggerHandler();    

    //Delete related action plans
    if (trigger.isdelete ){
        handler.OnBeforeDelete(trigger.old);        
    }    
    //Undelete related action plans
    else if (trigger.isUnDelete ){
        handler.OnAfterUndelete(trigger.new);       
    }
    // deleting the cases if Delete_Case__c sets to true
    else if (trigger.isUpdate && trigger.isAfter){        
        handler.OnAfterUpdate(trigger.new);
    }
}

/*********************************************************************************
CaseTriggerHandler  class:
----------------------------------------------
public without sharing class CaseTriggerHandler {
     List<Case>          deleteCasesList     =     new list<case>();
     // constructor
     public CaseTriggerHandler(){}     
     // Call on after update trigger to delete all cases where Delete_Case__c is true
     public void OnAfterUpdate(List<Case> ListCase){         
         for( Case c : ListCase){
                 // if Delete case is true add this record to list
                 if(c.Delete_Case__c){
                    Case c1=new Case(Id=c.Id);
                    deleteCasesList.add(c1);
                 }
            }
            // deleting the list items 
            if(deleteCasesList != null && deleteCasesList.size()>0){            
                 delete deleteCasesList;
            }
     }
     // Call on Before Delete trigger on Case Object
     public void OnBeforeDelete(List<Case> ListCase){     
        set<ID>             cIds    = new set<ID>();
        List<String>        apIds   = new List<String>();
        List<ActionPlan__c> deletePermantently_apIds= new List<ActionPlan__c>();       
        for( Case c : ListCase ){
            cIds.add( c.Id );
        }
        
        /* GET Action Plans to delete from recycle bin */
        deletePermantently_apIds = [ select Id, Name , LastModifiedDate from ActionPlan__c where Case__c in : cIds and isDeleted = true ALL ROWS ];
        
        if ( deletePermantently_apIds.size() >0 ){          
            Database.emptyRecycleBin(deletePermantently_apIds);
        }           
        
        //Get all action plans associated with Campaigns
        for( Case a : [Select (Select Id From Action_Plans__r) From Case a where Id in : cIds]){
            if (a.Action_Plans__r.size() >0 ){
                for(ActionPlan__c ap :a.Action_Plans__r ){
                    apIds.add(ap.Id);
                }
            }
        }
        if ( apIds.size() >0 ){
            ActionPlansBatchDelete aPBatch = new ActionPlansBatchDelete(apIds, Userinfo.getUserId());
            Database.ExecuteBatch( aPBatch );       
        }
    }
    // Call on After Undelete trigger on Case Object
    public void OnAfterUndelete(List<Case> ListCase){    
        set<ID>             cIds    = new set<ID>();           
        for( Case c : ListCase){
            cIds.add( c.Id );
        }
        list <ActionPlan__c> aPs = [ select Id from ActionPlan__c where Case__c in : cIds ALL ROWS ];
        
        try{
            if(ActionPlanObjectTriggerTest.isTest){
                //throw dmlException
                insert new Contact();   
            }
            //undelete aPs;
            Database.undelete( aPs,false);
        } catch ( Dmlexception e ){
            for (Case c: ListCase){
                c.addError('You can not undelete an action plan whose related object is deleted.');
            }
        }
    }
    
}