+ Start a Discussion
SFDC Apex DevSFDC Apex Dev 

Could any one help me increase the code coverage for this trigger....

TRIGGER
trigger Count_Activities on Event (after insert, after delete, after update) {
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
    if(trigger.isUpdate){        
            for(Event e:Trigger.new){
                objEventSet.add(e.whatId);

            }            
    }
  
    if(trigger.isInsert || trigger.isUndelete){        
            for(Event e:Trigger.new){
                objEventSet.add(e.whatId);
            }
    }

    if(trigger.isDelete){
        for(Event e:Trigger.old){
            objEventSet.add(e.whatId);

        }
    }    
    
    Map<Id,Opportunity> objOpp=new Map<Id,Opportunity>([Select Id,IsClosed,Activity_Due_Date__c from Opportunity where Id In :objEventSet]);

    if(trigger.isInsert || trigger.isUndelete){
        for(Event e:Trigger.new){          
            if(objOpp.containsKey(e.WhatId)){                
                if(e.ActivityDateTime >= System.now()){
                    if(oppMap.containsKey(e.WhatId)){
                         if(e.ActivityDateTime != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c < e.ActivityDateTime){
                            (oppMap.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        } else if (e.ActivityDateTime != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c == null){
                            (oppMap.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        }
                    }
                    else{
                         if(e.ActivityDateTime != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c < e.ActivityDateTime){
                            (objOpp.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        } else if (e.ActivityDateTime != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c == null){
                            (objOpp.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        }
                        if(objOpp.get(e.whatId).IsClosed==false){
                            oppMap.put(e.WhatId,objOpp.get(e.whatId));

                        }
                    }
                }
            }
        }
    }   

    if(trigger.isDelete){
        Map<Id,DateTime> opportunityByLatestDueDateMap = new Map<Id,DateTime>();
        List<Event> eventList = [Select Id, ActivityDateTime,WhatId from Event where whatId =: objOpp.Keyset() AND Id NOT IN : objEventSet limit 50000];
        for(Event et : eventList){
            if(opportunityByLatestDueDateMap.containsKey(et.WhatId)){
                if(opportunityByLatestDueDateMap.get(et.WhatId) < et.ActivityDateTime){
                    opportunityByLatestDueDateMap.put(et.WhatId,et.ActivityDateTime);
                }
            } else {
                opportunityByLatestDueDateMap.put(et.WhatId,et.ActivityDateTime);
            }
        }
        for(Event e:Trigger.old){
            if(objOpp.containsKey(e.WhatId)){
                if(e.ActivityDateTime >= System.now()){
                    if(oppMap.containsKey(e.WhatId)){
                        (oppMap.get(e.WhatId)).Activity_Due_Date__c = opportunityByLatestDueDateMap.get(e.WhatId);
                    }
                    else{
                        (objOpp.get(e.WhatId)).Activity_Due_Date__c = opportunityByLatestDueDateMap.get(e.WhatId);
                        if(objOpp.get(e.whatId).IsClosed==false){
                            oppMap.put(e.WhatId,objOpp.get(e.whatId));

                        }
                    }
                }
            }
         }
     }

    if(trigger.isUpdate){
        for(Event e:Trigger.new){
            if(objOpp.containsKey(e.WhatId)&& Trigger.oldMap.containsKey(e.Id)){
                    if(oppMap.containsKey(e.WhatId)){
                        if(e.ActivityDateTime != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c < e.ActivityDateTime){
                            (oppMap.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        } else if (e.ActivityDateTime != null && (oppMap.get(e.WhatId)).Activity_Due_Date__c == null){
                            (oppMap.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        }
                    }
                    else{
                        if(e.ActivityDateTime != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c < e.ActivityDateTime){
                            (objOpp.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDateTime;
                        } else if (e.ActivityDateTime != null && (objOpp.get(e.WhatId)).Activity_Due_Date__c == null){
                            (objOpp.get(e.WhatId)).Activity_Due_Date__c = e.ActivityDate;
                        }
                        if(objOpp.get(e.whatId).IsClosed==false){
                            oppMap.put(e.WhatId,objOpp.get(e.whatId));

                        }
                    }                
            }
        }    
    }
    if(!oppMap.isEmpty()){
        Database.SaveResult[] oppListstatus = Database.update(oppMap.values(), false);
        for(Database.SaveResult sr : oppListstatus) {
            for(Database.Error err : sr.getErrors()) {
                if(!sr.isSuccess()){
                    trigger.new[0].addError(err.getMessage());

                }
            }
        }
    }
}
TEST CLASS
@isTest
private class Count_ActivitiesTest{
     Static List<Event> lst = new List <Event>();    
     private static void data(){
        PI_Role__c pi = new PI_Role__c();
           pi.name='18';
           pi.Business_Unit__c='ABC';
           pi.Primary__c=true;
           pi.Role__c= 'AB Opp Queue';
           insert pi;
    }    
     private static void setupdata(){
        String canString  = Math.round(Math.random()*Math.pow(10, 7))+''+ Math.round(Math.random()*Math.pow(10, 4))+'';
        Account acc=new Account(Name='Accenture'+ canString.substring(0,4), SAP_Account__c = 'Test XYZ ACCOUNT');
        Database.insert(acc,true);
        
        OpportunityRecordTypeInfo__c recTypeInfo =New  OpportunityRecordTypeInfo__c();
        recTypeInfo.Name='PBBI Opportunity';
        recTypeInfo.Id__c= '01280000000AaAC';
        Database.insert(recTypeInfo,false);        
        Id recTypeId = recTypeInfo.Id__c;
        Date myDate = System.Today()+30;
        
        Opportunity opt = new Opportunity(Name = 'Test Opportunity 551', AccountId = acc.id, Type = 'Existing - Compliance', CloseDate = myDate,ForecastCategoryName = 'Pipeline', StageName = '2. Identify', Amount=12345, RecordTypeId = recTypeId ,Count_Activities__c=1 ,Vertical__c='Communications');
        Database.insert(opt,false);
        
        Opportunity opt1 = new Opportunity(Name = 'Test Opportunity 551', AccountId = acc.id, Type = 'Existing - Compliance', CloseDate = myDate,ForecastCategoryName = 'Pipeline', StageName = '2. Identify', Amount=12345, RecordTypeId = recTypeId ,Count_Activities__c=NULL ,Vertical__c='Communications');
        Database.insert(opt1,false);
        Event e = new Event(WhatId = opt.id, DurationInMinutes = 30, ActivityDateTime = system.now()+1, ActivityDate = System.Today() + 1);
        lst.add(e);
        Event f = new Event(WhatId = opt1.id, DurationInMinutes = 30, ActivityDateTime = system.now()+1, ActivityDate = System.Today() + 1);
        lst.add(f);
        Database.insert(lst,false);
        CustomSettingCreation.CreateCustomSettingData();
     }
     static testMethod void myTestMethod(){       
        data();
        setupdata();
        Test.startTest();
        system.assertEquals(2,lst.size());
        Test.stopTest(); 
     }
}

the bold, italic and underlined part not able to cover .. please help me
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class Count_ActivitiesTest{
     Static List<Event> lst = new List <Event>();    
     private static void data(){
        PI_Role__c pi = new PI_Role__c();
           pi.name='18';
           pi.Business_Unit__c='ABC';
           pi.Primary__c=true;
           pi.Role__c= 'AB Opp Queue';
           insert pi;
    }    
     private static void setupdata(){
        String canString  = Math.round(Math.random()*Math.pow(10, 7))+''+ Math.round(Math.random()*Math.pow(10, 4))+'';
        Account acc=new Account(Name='Accenture'+ canString.substring(0,4), SAP_Account__c = 'Test XYZ ACCOUNT');
        Database.insert(acc,true);
        
        OpportunityRecordTypeInfo__c recTypeInfo =New  OpportunityRecordTypeInfo__c();
        recTypeInfo.Name='PBBI Opportunity';
        recTypeInfo.Id__c= '01280000000AaAC';
        Database.insert(recTypeInfo,false);        
        Id recTypeId = recTypeInfo.Id__c;
        Date myDate = System.Today()+30;
        
        Opportunity opt = new Opportunity(Name = 'Test Opportunity 551', AccountId = acc.id, Type = 'Existing - Compliance', CloseDate = myDate,ForecastCategoryName = 'Pipeline', StageName = '2. Identify', Amount=12345, RecordTypeId = recTypeId ,Count_Activities__c=1 ,Vertical__c='Communications');
        Database.insert(opt,false);
        
        Opportunity opt1 = new Opportunity(Name = 'Test Opportunity 551', AccountId = acc.id, Type = 'Existing - Compliance', CloseDate = myDate,ForecastCategoryName = 'Pipeline', StageName = '2. Identify', Amount=12345, RecordTypeId = recTypeId ,Count_Activities__c=NULL ,Vertical__c='Communications');
        Database.insert(opt1,false);
        Event e = new Event(WhatId = opt.id, DurationInMinutes = 30, ActivityDateTime = system.now()+1, ActivityDate = System.Today() + 1);
        lst.add(e);
        Event f = new Event(WhatId = opt1.id, DurationInMinutes = 30, ActivityDateTime = system.now()+1, ActivityDate = System.Today() + 1);
        lst.add(f);
        Database.insert(lst,false);
        CustomSettingCreation.CreateCustomSettingData();
     }
     static testMethod void myTestMethod(){       
        data();
		
	
        setupdata();
        Test.startTest();
		Event e = [Select Id ,WhatId ,ActivityDateTime from Event Limit 1 ] ; 
		e.ActivityDateTime =System.today()+2 ;
		update e ; 
		
		
		delete e ; 
		
		undelete e ; 
		
        system.assertEquals(2,lst.size());
        Test.stopTest(); 
     }
}