+ Start a Discussion
ashu 6112ashu 6112 

testclass @trigger helper

Hi All,

I have to write test class for the below trigger helper class.  NEED HELP.




public class CableDealHeaderTriggerHandler {
    
    //method to update all related Deal Request
    public static void updateDealRequest(List<Deal_Request_Header__c> drhNewList, Map<id,Deal_Request_Header__c>drhOldMap){
        List<Cable_Deal_Request__c> cdrListUpdate = new List<Cable_Deal_Request__c>();
        
        //Query on Deal Request Header with Inner Query to get all related Deal Request
        List<Deal_Request_Header__c> drhList = [select id,name,Cable_Ownership__c, Cable_Program_Category__c,
                                                Cable_Network__c,Cable_Requested_By__c,Cable_Project_Phase__c,Cable_Program_Type__c,Cable_Genre__c,
                                                Cable_Program_Format__c,Cable_Program_Length__c, Cable_Camera_View__c, Deal_Request_Date__c,
                                                Cable_Submitted_Date__c,
                                                (select id,Cable_Network_Platform__c,Cable_Requesting_Executive__c,Cable_Project_Phase__c,Cable_Ownership__c,
                                                Cable_Program_Type__c,Cable_Program_Category__c,Genre__c,Cable_Program_Format__c,Cable_Program_Length__c,
                                                Cable_Camera_View__c,Deal_Request_Date__c,Cable_Submitted_Date__c from Cable_Deal_Requests__r)
                                                from Deal_Request_Header__c where id in : drhNewList];
        
        //For loop to iterate and check if fields are saved or not                                        
        for(Deal_Request_Header__c drh : drhList){
            if( (drh.Cable_Ownership__c != drhOldMap.get(drh.id).Cable_Ownership__c) || (drh.Cable_Program_Category__c != drhOldMap.get(drh.id).Cable_Program_Category__c) || 
                (drh.Cable_Network__c != drhOldMap.get(drh.id).Cable_Network__c) || (drh.Cable_Requested_By__c != drhOldMap.get(drh.id).Cable_Requested_By__c) ||
                (drh.Cable_Project_Phase__c != drhOldMap.get(drh.id).Cable_Project_Phase__c) || (drh.Cable_Program_Type__c != drhOldMap.get(drh.id).Cable_Program_Type__c) ||
                (drh.Cable_Program_Type__c != drhOldMap.get(drh.id).Cable_Program_Type__c) || (drh.Cable_Genre__c != drhOldMap.get(drh.id).Cable_Genre__c) ||
                (drh.Cable_Program_Format__c != drhOldMap.get(drh.id).Cable_Program_Format__c) || (drh.Cable_Program_Length__c != drhOldMap.get(drh.id).Cable_Program_Length__c) ||
                (drh.Cable_Camera_View__c != drhOldMap.get(drh.id).Cable_Camera_View__c) || (drh.Deal_Request_Date__c != drhOldMap.get(drh.id).Deal_Request_Date__c) ||
                (drh.Cable_Submitted_Date__c != drhOldMap.get(drh.id).Cable_Submitted_Date__c))
            {
                if(drh.Cable_Deal_Requests__r.size()>0){
                    //for loop on related Deal Request
                    for(Cable_Deal_Request__c cdr : drh.Cable_Deal_Requests__r){
                        if(drh.Cable_Network__c != drhOldMap.get(drh.id).Cable_Network__c)
                            cdr.Cable_Network_Platform__c = drh.Cable_Network__c;
                        if(drh.Cable_Requested_By__c != drhOldMap.get(drh.id).Cable_Requested_By__c)
                            cdr.Cable_Requesting_Executive__c = drh.Cable_Requested_By__c;
                        if(drh.Cable_Project_Phase__c != drhOldMap.get(drh.id).Cable_Project_Phase__c)
                            cdr.Cable_Project_Phase__c = drh.Cable_Project_Phase__c;
                        if(drh.Cable_Ownership__c != drhOldMap.get(drh.id).Cable_Ownership__c)
                            cdr.Cable_Ownership__c = drh.Cable_Ownership__c;
                        if(drh.Cable_Program_Type__c != drhOldMap.get(drh.id).Cable_Program_Type__c)
                            cdr.Cable_Program_Type__c = drh.Cable_Program_Type__c;
                        if(drh.Cable_Program_Category__c != drhOldMap.get(drh.id).Cable_Program_Category__c)
                            cdr.Cable_Program_Category__c = drh.Cable_Program_Category__c;
                        if(drh.Cable_Genre__c != drhOldMap.get(drh.id).Cable_Genre__c)
                            cdr.Genre__c = drh.Cable_Genre__c;
                        if(drh.Cable_Program_Format__c != drhOldMap.get(drh.id).Cable_Program_Format__c)
                            cdr.Cable_Program_Format__c = drh.Cable_Program_Format__c;
                        if(drh.Cable_Program_Length__c != drhOldMap.get(drh.id).Cable_Program_Length__c)
                            cdr.Cable_Program_Length__c = drh.Cable_Program_Length__c;
                        if(drh.Cable_Camera_View__c != drhOldMap.get(drh.id).Cable_Camera_View__c)
                            cdr.Cable_Camera_View__c = drh.Cable_Camera_View__c;
                        if(drh.Deal_Request_Date__c != drhOldMap.get(drh.id).Deal_Request_Date__c)
                            cdr.Deal_Request_Date__c = drh.Deal_Request_Date__c;
                        if(drh.Cable_Submitted_Date__c != drhOldMap.get(drh.id).Cable_Submitted_Date__c)
                            cdr.Cable_Submitted_Date__c = drh.Cable_Submitted_Date__c;
                        cdrListUpdate.add(cdr);
                    }
                }
                
            }
        }
        
        if(cdrListUpdate.size()>0)
            update cdrListUpdate;
    }
}
monikamonika
something alon these lines
@isTest
public class CableDealHeaderTriggerHandler_test{
    @isTest
    publlic static void test_coverage(){

    Deal_Request_Header__c newRequest =  new Deal_Request_Header__c();
    newRequest.Cable_Genre__c ='aaa';
    newRequest.Cable_Network_Platform__c ='bbb';
    //add some other fields 
    //assuming your trigger  method wil not run on insert
    insert newRequest;
    
     ///// A - either run the method in the trigger 
    List<Deal_Request_Header__c> drhNewList = new  List<Deal_Request_Header__c>();
    Map<id,Deal_Request_Header__c>drhOldMap  = new  Map<id,Deal_Request_Header__c>();
    drhNewList.add(newRequest);

    Deal_Request_Header__c oldRequest =  new Deal_Request_Header__c(id= newRequest.ID);
    drhOldMap.put(newRequest.id, oldRequest);

    CableDealHeaderTriggerHandler.updateDealRequest( drhNewList, drhOldMap);
    /
    /////B - or run the trigger  
    newRequest.Deal_Request_Date__c = date.today();
    update newRequest;


    ///check the values were copied to the fields
    Deal_Request_Header__c check = [SELECT Cable_Network__c,Cable_Network_Platform__c FROM  Deal_Request_Header__c];
    //add some other fields 
    system.assertEquals( check.Cable_Network__c, check.Cable_Network_Platform__c);
    system.assertEquals( check.Genre__c, check.Cable_Genre__c);
    }
}
ashu 6112ashu 6112
The trigger for the bove class is :

trigger CableDealHeaderTrigger on Deal_Request_Header__c (After Update) {
 
    if(CableDealRequestTriggerHandler.isCurrentlyInExecAfterHeaderTrigger == false && (Trigger.New[0].Cable_Deal_Header_Status__c == 'Submitted')&&(Trigger.Old[0].Cable_Deal_Header_Status__c == 'Open')){
    
    system.debug('++++++' + DealRequestTriggerHandler.isCurrentlyInExecAfter);
    
    //cabledealrequestcontroller CableClass = New cabledealrequestcontroller();
    DealRequestTriggerHandler.isCurrentlyInExecAfter = True;
     system.debug('++++++' + DealRequestTriggerHandler.isCurrentlyInExecAfter);
    CableDealRequestContoller.DealApprovedByHod(Trigger.New[0].Id);
    
    
    }
    
    //added by nitish to update all child Deal Request on update of header
    if(trigger.isUpdate && trigger.isAfter){
        CableDealHeaderTriggerHandler.updateDealRequest(trigger.new, trigger.oldMap);
    }
    
}