+ Start a Discussion
Priya134Priya134 

Test class code coverage only 74%

Please help with the test class code coverage : I have only 74%.

public with sharing class TC_AttachmentTriggerHelper {
    public static void after (List<Attachment> triggerList) {
        Set<Id> oppIds = new Set<Id>();
        Set<Id> cSignIds = new Set<Id>();
        Set<Id> sign2Ids = new Set<Id>();
        Set<Id> allSignIds = new Set<Id>();
        Set<Id> ccgIds = new Set<Id>();
        List<Opportunity> oppList = new List<Opportunity>();
        Map<id,Opportunity> oppMap = new Map<id,Opportunity>();  
                
        //get all oppIds
        for (Attachment att: triggerList ) {
            if(att.ParentId.getSObjectType().getDescribe().getName() == 'Opportunity'){
                oppIds.add(att.ParentId);
            }
        }
                
        //query all Attachments for all opps
        List<Attachment> all = [SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN :oppIds];
        
        for (Attachment att: all ) {
            if(att.ParentId.getSObjectType().getDescribe().getName() == 'Opportunity'){
                system.debug('CCG 1----->');
                if(att.Name.contains('-CSign')){
                    cSignIds.add(att.ParentId);
                }else if(att.Name.contains('-Sign2')){
                    sign2Ids.add(att.ParentId);
                }else if(att.Name.contains('-AllSign')){
                    allSignIds.add(att.ParentId);
                }else if(att.Name.contains('CCG')){
                    ccgIds.add(att.ParentId);
                }else if(att.Name.contains('1 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '1'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('2 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '2'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('3 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '3'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('4 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '4'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('5 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '5'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('6 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '6'));
                    oppMap.putall(oppList); 
                }                            
            }
        }
        
 
        if(oppIds.size() > 0){
            List<Opportunity> opps = [SELECT Id, CSign__c FROM Opportunity WHERE Id IN:oppIds];
            
            for(Opportunity opp : opps){ 
                              
                opp.CSign__c = cSignIds.contains(opp.Id) ? TRUE : FALSE;  
                opp.sign2__c = sign2Ids.contains(opp.Id) ? TRUE : FALSE;  
                opp.allSign__c = allSignIds.contains(opp.Id) ? TRUE : FALSE;       
                if(!ccgIds.contains(opp.Id)){
                opp.CCG__c = ' ';
                }                  
            }
            
            update opps;
        }
               
         if(oppMap.size()>0){
         update oppMap.values();
         }          
    }    
}

@isTest
public class TC_AttachmentTest {
    @isTest
    static void testGenerateRecords() {
        Opportunity opp = new Opportunity();
        opp.StageName = 'test';
        opp.Name = 'test';
        opp.CCG__c = '';
        opp.CloseDate = Date.today();
        
        insert opp;
        
        
        Attachment att = new Attachment();
        att.ParentId = opp.Id;
        att.Name = 'foo-CSign.pdf';
        att.Body = Blob.valueOf('xxxx');
        
        insert att;
        
        att.Name = 'foo.pdf';
        update att;
        
        att.Name = 'CCG.pdf';
        update att;
        
        att.Name = '-Sign2.pdf';
        update att;
        
        att.Name = '-AllSign.pdf';
        update att;
        
        att.Name = '1 CCG.pdf';
        update att;        
        opp.CCG__c = '1';
        update opp;
        
    }

}
Suraj Tripathi 47Suraj Tripathi 47
Hi Priya,
 
//Attachment Trigger

trigger AttachmnetTrigger on Attachment (after insert,after update) {
    if(trigger.isAfter && trigger.isInsert){
        TC_AttachmentTriggerHelper.after(trigger.new);
    }
    if(trigger.isAfter && trigger.isUpdate){
        TC_AttachmentTriggerHelper.after(trigger.new);
    }
}




//Test Class

@istest
public class TC_AttachmentTriggerHelper_test {
@isTest
    static void testGenerateRecords() {
        Opportunity opp = new Opportunity();
        opp.StageName = 'test';
        opp.Name = 'test';
        opp.CCG__c = '';
        opp.CloseDate = Date.today();
        
        insert opp;
        
        
        Attachment att = new Attachment();
        att.ParentId = opp.Id;
        att.Name = 'foo-CSign.pdf';
        att.Body = Blob.valueOf('xxxx');
        
        insert att;
        
        att.Name = 'foo.pdf';
        update att;
        
        att.Name = 'CCG.pdf';
        update att;
        
        att.Name = '-Sign2.pdf';
        update att;
        
        att.Name = '-AllSign.pdf';
        update att;
        
        att.Name = '1 CCG.pdf';
        update att;        
        
        att.Name = '2 CCG.pdf';
        update att; 
        att.Name = '3 CCG.pdf';
        update att;
        att.Name = '4 CCG.pdf';
        update att;
        att.Name = '5 CCG.pdf';
        update att;
        att.Name = '6 CCG.pdf';
        update att;
    }
   
}




//Apex class

public with sharing class TC_AttachmentTriggerHelper {
    public static void after (List<Attachment> triggerList) {
        Set<Id> oppIds = new Set<Id>();
        Set<Id> cSignIds = new Set<Id>();
        Set<Id> sign2Ids = new Set<Id>();
        Set<Id> allSignIds = new Set<Id>();
        Set<Id> ccgIds = new Set<Id>();
        List<Opportunity> oppList = new List<Opportunity>();
        Map<id,Opportunity> oppMap = new Map<id,Opportunity>();  
                
        //get all oppIds
        for (Attachment att: triggerList ) {
            if(att.ParentId.getSObjectType().getDescribe().getName() == 'Opportunity'){
                oppIds.add(att.ParentId);
            }
        }
                
        //query all Attachments for all opps
        List<Attachment> all = [SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN :oppIds];
        
        for (Attachment att: all ) {
            if(att.ParentId.getSObjectType().getDescribe().getName() == 'Opportunity'){
                system.debug('CCG 1----->');
                if(att.Name.contains('-CSign')){
                    cSignIds.add(att.ParentId);
                }else if(att.Name.contains('-Sign2')){
                    sign2Ids.add(att.ParentId);
                }else if(att.Name.contains('-AllSign')){
                    allSignIds.add(att.ParentId);
                }else if(att.Name.contains('1 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '1'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('2 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '2'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('3 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '3'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('4 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '4'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('5 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '5'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('6 CCG')){                    
                    oppList.add(new Opportunity(id=att.ParentId,CCG__c = '6'));
                    oppMap.putall(oppList); 
                }else if(att.Name.contains('CCG')){
                    ccgIds.add(att.ParentId);
                }                            
            }
        }
        if(oppIds.size() > 0){
            List<Opportunity> opps = [SELECT Id, CSign__c FROM Opportunity WHERE Id IN:oppIds];
            
            for(Opportunity opp : opps){ 
                              
                opp.CSign__c = cSignIds.contains(opp.Id) ? TRUE : FALSE;  
                opp.sign2__c = sign2Ids.contains(opp.Id) ? TRUE : FALSE;  
                opp.allSign__c = allSignIds.contains(opp.Id) ? TRUE : FALSE;       
                if(!ccgIds.contains(opp.Id)){
                opp.CCG__c = ' ';
                }                  
            }
            
            update opps;
        }
               
         if(oppMap.size()>0){
         update oppMap.values();
         }          
    }
}


Hi priya, I have made some changes to your code.

User-added image
I have written this condition below in the code after that it started working properly.



Please Mark it as Best Answer if it helps.
Thanks