You need to sign in to do that
Don't have an account?
ranga 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 !!!
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 !!!
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.
All Answers
It may require some test data and minor changes. Please find the same in below code and let me know if it helps!
Modified your test class 'GroupClusterMapTest' by adding below pieces of code!
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');
}
}
}
}
}
}
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.