function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ranga babu vangalapudi 2ranga babu vangalapudi 2 

How to cover test class for if and for loop's in triggers

Following is my Before trigger:

trigger GroupClusterMap on Account (before insert, before update) {
    set<string> groupId = new set<string>();
    list<Account> listAccs= new list<Account>();
    list<Outlets_Groups_Primary_Customer__c> groupOutlets = new list<Outlets_Groups_Primary_Customer__c>();
    for(Account acc:trigger.new) {
        if(acc.Group_Name1__c!=NULL)
            groupId.add(acc.Group_Name1__c);
        }
    listAccs = [Select Id, Cluster_Code__c,Group_Name1__c,TL_Code__c from Account where Group_Name1__c IN: groupId limit 1];
    groupOutlets = [Select Id, Lead_TL__c,Name from Outlets_Groups_Primary_Customer__c where Id IN: groupId limit 1];
    if(trigger.isinsert) {
        for(Account aa:trigger.new) {
            for(Account dup:listAccs) {
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c)){
                    aa.adderror('Group is mapped to a Cluster');
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                    for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                        if(outgrps.Lead_TL__c == NULL || outgrps.Lead_TL__c == '')
                            aa.adderror('Please do a Lead TL mapping in the Group');
                    }
                }                    
                }
                }
            }
        }
    if(trigger.isupdate) {
        for(Account aa:trigger.new){
            for(Account dup:listAccs) {                             
               // if ((aa.Group_Name1__c==dup.Group_Name1__c && aa.Group_Name1__c !=trigger.oldmap.get(aa.id).Group_Name1__c) && (aa.Cluster_Code__c!=dup.Cluster_Code__c && aa.Cluster_Code__c !=trigger.oldmap.get(aa.id).Cluster_Code__c))
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c)){
                    aa.adderror('Group is mapped to a Cluster');
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                    for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                        if(outgrps.Lead_TL__c == NULL || outgrps.Lead_TL__c == '')
                            aa.adderror('Please do a Lead TL mapping in the Group');
                    }
                  }                    
                }
            } 
        }
    }
}
=========================================================
Following is the test class i had written for it - it only covers 57% code.

@IsTest
public class GroupClusterMapTest {   
    
    static testMethod void GroupClusterMapTest(){
        Id RecordTypeId = Schema.SObjectType.Masters__c.getRecordTypeInfosByName().get('Cluster').getRecordTypeId();
        Masters__c master = New Masters__c();
        master.Cluster_Name__c='RPCSCL004';
        master.Geographic_State__c='ANDHRA PRADESH';
        master.RecordTypeid=RecordTypeId;
        insert master;        
        
        TL_Master__c tl1= new TL_Master__c();
        tl1.Name='APTL004';
        tl1.TL_Territory_Name__c='NORTH COASTAL';
        tl1.Cluster__c=master.id;
        insert tl1;
        
        TSE_Master__c oldTse2 = new TSE_Master__c();
        oldTse2.Name = 'APTS033';
        oldTse2.TSE_Territory_Name__c = 'VISAKHAPATNAM - II AREA-I';
        oldTse2.TL_Code__c=tl1.id;
        insert oldTse2;
        
        Beat_TSE_Mapping__c NewBte1 = new Beat_TSE_Mapping__c();
        NewBte1.Beat_Name__c = 'BEAT 03';
        NewBte1.Name = 'ABBT1130';
        NewBte1.TSE_Code__c = oldTse2.id;
        insert NewBte1;
        
        Outlets_Groups_Primary_Customer__c newGrp = new Outlets_Groups_Primary_Customer__c();
        newGrp.Name = 'mygroup';
        newGrp.Type__c = 'Groups';
        newGrp.State_Group_PC__c = 'ANDHRA PRADESH';
        newGrp.Lead_TL__c = tl1.Id;
        insert newGrp;
        
        Account OldAcc = new Account();        
        OldAcc.Beat_Code__c = NewBte1.Id;
        //OldAcc.Beat_Name__c = oldBte.Beat_Name__c ;
        OldAcc.TSE_Code__c = oldTse2.id;
        //OldAcc.TSE_ID__c = oldBte.TSE_ID__c;
        //OldAcc.TSE_Name__c = oldBte.TSE_Name__c;
        OldAcc.District__c = 'Test District';
        OldAcc.Goegraphy_state__c = 'ANDHRA PRADESH';
        OldAcc.Town__c = 'Test Town';
        OldAcc.Name = 'Test name';
        OldAcc.Market_Type__c = 'Corporation';
        OldAcc.MSL_Town_Tier__c= 'BTNTest';
        OldAcc.Cluster_Code__c=master.Id;
        OldAcc.TSE_Code__c=oldTse2.id;
        OldAcc.Outlet_Type__c='D';
        OldAcc.Beat_Code__c= NewBte1.Id;
        OldAcc.TL_Code__c = tl1.Id;
        OldAcc.Address__c = 'testadd';
        OldAcc.Stock_List__c = 'Tier 1';
        OldAcc.Channel_type__c = 'PrOP';
        OldAcc.Micro_channel__c = 'Prestige Bar';
        OldAcc.Perfect_Outlet__c = 'Yes';
        OldAcc.Group_Outlet__c='No';
        oldAcc.Group_Name1__c = newGrp.Id;
        OldAcc.Contract_type__c = 'Open';
        insert OldAcc;
        
         Account NewAcc = new Account();
        NewAcc.id = OldAcc.id;
        NewAcc.Beat_Code__c = NewBte1.ID;
        //  NewAcc.Beat_Name__c = NewBte.Beat_Name__c ; 
        //NewAcc.TSE_Code__c = oldTse3.id;
        // NewAcc.TSE_ID__c = NewBte.TSE_ID__c;
        // NewAcc.TSE_Name__c = NewBte.TSE_Name__c;
        NewAcc.District__c = 'Test District1';
        NewAcc.Town__c = 'Test Town1';
        NewAcc.Name = 'Test name1';
        oldAcc.Group_Name1__c = newGrp.Id;
        update NewAcc;
    }

}

Your help is greatly appreciated...Thank you !!!
 
Best Answer chosen by ranga babu vangalapudi 2
edanna kedanna k
Dear ranga babu vangalapudi 2,

You have to create 2 methods in your test class
 1st method - insert account with Group_Outlet__c = No.
 2nd method - insert account with Group_Outlet__c = yes.
//Use above test method GroupClusterMapTest() as 1st method as it contains Group_Outlet__c = No.
static testMethod void GroupClusterMapTest(){
	OldAcc.Group_Outlet__c='No';
}
//Create 2nd method to insert account with Group_Outlet__c = yes. (2nd method has same code as 1st method with below diff line of code)
static testMethod void GroupClusterMapTest(){
	OldAcc.Group_Outlet__c='Yes';
}

 

All Answers

edanna kedanna k
Dear ranga babu vangalapudi 2,

It may require some test data and minor changes.  Please find the same in below code and let me know if it helps! 
@IsTest
public class GroupClusterMapTest {   
    
    static testMethod void GroupClusterMapTest(){
        Id RecordTypeId = Schema.SObjectType.Masters__c.getRecordTypeInfosByName().get('Cluster').getRecordTypeId();
        Masters__c master = New Masters__c();
        master.Cluster_Name__c='RPCSCL004';
        master.Geographic_State__c='ANDHRA PRADESH';
        master.RecordTypeid=RecordTypeId;
        insert master; 
		
//Create one more master record to pass the condition  "aa.Cluster_Code__c!=dup.Cluster_Code__c" in IF.
Masters__c master2 = New Masters__c();
master2.Cluster_Name__c='RPCSCL004';
master2.Geographic_State__c='Telangana';
master2.RecordTypeid=RecordTypeId;
insert master2; 		
        
        TL_Master__c tl1= new TL_Master__c();
        tl1.Name='APTL004';
        tl1.TL_Territory_Name__c='NORTH COASTAL';
        tl1.Cluster__c=master.id;
        insert tl1;
		
//Create one more master record to pass the condition  "aa.TL_Code__c!=dup.TL_Code__c" in IF.		
TL_Master__c tl2= new TL_Master__c();
tl2.Name='APTL004';
tl2.TL_Territory_Name__c='NORTH COASTAL';
tl2.Cluster__c=master2.id;
insert tl2;

		
        TSE_Master__c oldTse2 = new TSE_Master__c();
        oldTse2.Name = 'APTS033';
        oldTse2.TSE_Territory_Name__c = 'VISAKHAPATNAM - II AREA-I';
        oldTse2.TL_Code__c=tl1.id;
        insert oldTse2;
        
        Beat_TSE_Mapping__c NewBte1 = new Beat_TSE_Mapping__c();
        NewBte1.Beat_Name__c = 'BEAT 03';
        NewBte1.Name = 'ABBT1130';
        NewBte1.TSE_Code__c = oldTse2.id;
        insert NewBte1;
        
        Outlets_Groups_Primary_Customer__c newGrp = new Outlets_Groups_Primary_Customer__c();
        newGrp.Name = 'mygroup';
        newGrp.Type__c = 'Groups';
        newGrp.State_Group_PC__c = 'ANDHRA PRADESH';
		
//newGrp.Lead_TL__c = null -> so that "if(outgrps.Lead_TL__c == NULL || outgrps.Lead_TL__c == '')" will be true.
newGrp.Lead_TL__c = null;
		
        insert newGrp;
        
        Account OldAcc = new Account();        
        OldAcc.Beat_Code__c = NewBte1.Id;
        //OldAcc.Beat_Name__c = oldBte.Beat_Name__c ;
        OldAcc.TSE_Code__c = oldTse2.id;
        //OldAcc.TSE_ID__c = oldBte.TSE_ID__c;
        //OldAcc.TSE_Name__c = oldBte.TSE_Name__c;
        OldAcc.District__c = 'Test District';
        OldAcc.Goegraphy_state__c = 'ANDHRA PRADESH';
        OldAcc.Town__c = 'Test Town';
        OldAcc.Name = 'Test name';
        OldAcc.Market_Type__c = 'Corporation';
        OldAcc.MSL_Town_Tier__c= 'BTNTest';
        OldAcc.Cluster_Code__c=master.Id;
        OldAcc.TSE_Code__c=oldTse2.id;
        OldAcc.Outlet_Type__c='D';
        OldAcc.Beat_Code__c= NewBte1.Id;
        OldAcc.TL_Code__c = tl1.Id;
        OldAcc.Address__c = 'testadd';
        OldAcc.Stock_List__c = 'Tier 1';
        OldAcc.Channel_type__c = 'PrOP';
        OldAcc.Micro_channel__c = 'Prestige Bar';
        OldAcc.Perfect_Outlet__c = 'Yes';
        OldAcc.Group_Outlet__c='No';
        oldAcc.Group_Name1__c = newGrp.Id;
        OldAcc.Contract_type__c = 'Open';
        insert OldAcc;
        
         Account NewAcc = new Account();
        NewAcc.id = OldAcc.id;
        NewAcc.Beat_Code__c = NewBte1.ID;
        //  NewAcc.Beat_Name__c = NewBte.Beat_Name__c ; 
        //NewAcc.TSE_Code__c = oldTse3.id;
        // NewAcc.TSE_ID__c = NewBte.TSE_ID__c;
        // NewAcc.TSE_Name__c = NewBte.TSE_Name__c;
        NewAcc.District__c = 'Test District1';
        NewAcc.Town__c = 'Test Town1';
        NewAcc.Name = 'Test name1';
NewAcc.Cluster_Code__c = master2.id;
NewAcc.TL_Code__c = tl2.Id;
        oldAcc.Group_Name1__c = newGrp.Id;
        update NewAcc;
    }

}

Modified your test class 'GroupClusterMapTest' by adding below pieces of code!
Masters__c master2 = New Masters__c();
master2.Cluster_Name__c='RPCSCL004';
master2.Geographic_State__c='Telangana';
master2.RecordTypeid=RecordTypeId;
insert master2;
TL_Master__c tl2= new TL_Master__c();
tl2.Name='APTL004';
tl2.TL_Territory_Name__c='NORTH COASTAL';
tl2.Cluster__c=master2.id;
insert tl2;
newGrp.Lead_TL__c = null;
NewAcc.Cluster_Code__c = master2.id;
NewAcc.TL_Code__c = tl2.Id;



 
ranga babu vangalapudi 2ranga babu vangalapudi 2
Hi Edanna, 

Thank you for your reply, unforntunately due some new requirements came in these two days i changed my code slightly.

Can you please help me on this, Following is the new code

trigger GroupClusterMap on Account (before insert, before update) {
    set<string> groupId = new set<string>();
    list<Account> listAccs= new list<Account>();
    list<Outlets_Groups_Primary_Customer__c> groupOutlets = new list<Outlets_Groups_Primary_Customer__c>();
    for(Account acc:trigger.new) {
        if(acc.Group_Name1__c!=NULL)
            groupId.add(acc.Group_Name1__c);
        }
    listAccs = [Select Id, Cluster_Code__c,Group_Name1__c,TL_Code__c from Account where Group_Name1__c IN: groupId limit 1];
   groupOutlets = [Select Id, Lead_TL__c,Name from Outlets_Groups_Primary_Customer__c where Id IN: groupId limit 1];
    if(trigger.isinsert) {
        for(Account aa:trigger.new) {             
            for(Account dup:listAccs) {
                if(aa.Group_Outlet__c == 'Yes' && aa.Group_Name1__c != NULL){
                aa.Group_Name1__c.addError('Group Name is needed');
                    }
                if(aa.Group_Outlet__c == 'No' && aa.Group_Name1__c != NULL){
                aa.Group_Name1__c.addError('Group Name should be blank');
                    }
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c))
                      aa.Group_Name1__c.addError('Group is mapped to a Cluster');
                  if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                                for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                                    if(outgrps.Lead_TL__c == NULL)
                                            aa.Group_Name1__c.addError('Please do a Lead TL mapping in the Group');
                    }
                }
            } 
        }
    }
    if(trigger.isupdate) {
        for(Account aa:trigger.new){
            for(Account dup:listAccs) { 
                 if(aa.Group_Outlet__c == 'Yes' && aa.Group_Name1__c == NULL){
                     system.debug(aa.Group_Name1__c);
                aa.Group_Name1__c.addError('Group Name is needed');
                    }
                else if(aa.Group_Outlet__c == 'No' && aa.Group_Name1__c != NULL){
                    system.debug('*****');
                aa.Group_Name1__c.addError('Group Name should be blank');
                    }
               // if ((aa.Group_Name1__c==dup.Group_Name1__c && aa.Group_Name1__c !=trigger.oldmap.get(aa.id).Group_Name1__c) && (aa.Cluster_Code__c!=dup.Cluster_Code__c && aa.Cluster_Code__c !=trigger.oldmap.get(aa.id).Cluster_Code__c))
              else  if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c))
                      aa.Group_Name1__c.addError('Group is mapped to a Cluster');
              else    if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                            for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                                        if(outgrps.Lead_TL__c == NULL)
                                            aa.Group_Name1__c.addError('Please do a Lead TL mapping in the Group');
                    }
                }
            } 
        }
    }
}
 
edanna kedanna k
Dear ranga babu vangalapudi 2,

You have to create 2 methods in your test class
 1st method - insert account with Group_Outlet__c = No.
 2nd method - insert account with Group_Outlet__c = yes.
//Use above test method GroupClusterMapTest() as 1st method as it contains Group_Outlet__c = No.
static testMethod void GroupClusterMapTest(){
	OldAcc.Group_Outlet__c='No';
}
//Create 2nd method to insert account with Group_Outlet__c = yes. (2nd method has same code as 1st method with below diff line of code)
static testMethod void GroupClusterMapTest(){
	OldAcc.Group_Outlet__c='Yes';
}

 
This was selected as the best answer