• Alina Kondrasheva
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    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.');
            }
        }
    }
    
}

Hello,

 

I've been running in circles trying to figure out how to create a test class for my trigger.  The trigger works great in my sandbox, but in order for me to put it into production, I need to test it.  I've installed the Force.com IDE, but I'm not sure how to go about creating the instance within the test class. 

 

Below is my code, can anyone help me create a test class for this or point me in the right direction?

 

Trigger CloseSalesOrder on Invoice__c(after insert)

{

Set<Id> ids = trigger.newmap.keySet();

List<Invoice__c> inv=[select Sales_Order_Number__r.Status__c from  Invoice__c where id IN :ids];

List< Sales_Order__c> updateSalesorder=new List< Sales_Order__c>();

For(Invoice__c invs:inv)

{

 Invs.Sales_Order_Number__r.Status__c='Closed';

updateSalesorder.add(Invs.Sales_Order_Number__r);

}

Update updateSalesorder;

}