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
@anilbathula@@anilbathula@ 

test coverage decreased

hi guys,

This trigger has 100% test coverage before after adding one condition it decreased .

help me in increasing the test coverage.

 
trigger Updtcon on comments__c (before insert) {
Set<Id> sobjectSetOfIds = new Set<Id>();
Set<Id> sobjectSetOfctctIds = new Set<Id>();
Set<Id> sobjectSetOffinIds = new Set<Id>();

Comments__c cms;
opportunity opp;
UserRole ur = [ select Id, Name FROM UserRole where Id =:Userinfo.getUserroleid()];


for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c!=null && cms.Contact__c==null && cms.finance__c==null){
sobjectSetOfIds.add(cms.Opportunity__c);
}
if(cms.Opportunity__c==null && cms.Contact__c!=null && cms.finance__c==null){
sobjectSetOfctctIds.add(cms.Contact__c);
}
if(cms.Opportunity__c==null && cms.Contact__c==null && cms.finance__c!=null){
sobjectSetOffinIds.add(cms.Finance__c);
}
}
/* updating contact and finance from opportunity object comments related list*/
if(!sobjectSetOfIds.ISEmpty())
{
Map<Id,Opportunity>smap= new Map<Id, Opportunity>([Select id,Name,Borrower__c,(Select id,name from Finances__r) from Opportunity where Borrower__r.RecordType.name=:'Applicant' AND Id in : sobjectSetOfIds ]);

for(Comments__c s: trigger.new){
if(smap.containsKey(s.Opportunity__c)){ 
s.Contact__c=smap.get(s.Opportunity__c).Borrower__c;
if(smap.get(s.Opportunity__c).finances__r.size()>0){
s.Finance__c=smap.get(s.Opportunity__c).finances__r[0].id;
}
s.Comment_Created_from__c ='Opportunity ::'+ ' '+smap.get(s.Opportunity__c).Name;
s.written_by__c='Opportunity';//UserInfo.getName();
s.Written_date__c=system.Today();
s.Role__c=ur.name;
}
}
}

/* updating opportunity and finance from contact object comments related list*/
if(!sobjectSetOfctctIds.ISEmpty()){
List<Opportunity> opportunities = [select Id,Borrower__c, Name,Borrower__r.Name,(Select id,name,contact__c from Finances__r )from Opportunity where Borrower__r.RecordType.name=:'Applicant' AND Borrower__c in:sobjectSetOfctctIds ];
map<string,string> mpcopid=new map<string,string>();
map<string,string> mpcopname=new map<string,string>();
map<string,string> mpfinid=new map<string,string>();
for(Opportunity oppn:opportunities){
mpcopid.put(oppn.Borrower__c,oppn.Id);
mpcopname.put(oppn.Borrower__c,oppn.Borrower__r.Name);
if(oppn.finances__r.size()>0){mpfinid.put(oppn.Borrower__c,oppn.Finances__r[0].Id);}


}
for(Comments__c s: trigger.new){
if(mpcopid.containsKey(s.Contact__c))
{
s.Opportunity__c=mpcopid.get(s.Contact__c);
if(mpfinid.ContainsKey(s.Contact__c)){s.Finance__c=mpfinid.get(s.Contact__c);}
s.Comment_Created_from__c ='Contact :: '+mpcopname.get(s.Contact__c);
s.written_by__c='Contact';//UserInfo.getName();
s.Written_date__c=system.Today();
s.Role__c=ur.name;
}
}
}



/* updating opportunity and contact from finance */
if(!sobjectSetOffinIds.ISEmpty()){
Map<id,Finance__c> fc =new Map<id, Finance__c>([select id,Name,Opportunity__r.id,Opportunity__r.Borrower__r.id from Finance__c where id in:sobjectSetOffinIds]);
for(Comments__c cs:trigger.New){
if(fc.containsKey(cs.Finance__c)){
cs.Opportunity__c=fc.get(cs.Finance__c).opportunity__r.id;
cs.Contact__c=fc.get(cs.Finance__c).Opportunity__r.Borrower__r.id;
cs.Comment_Created_from__c='Finance::'+''+fc.get(cs.Finance__c).Name;
cs.written_by__c='Finance';//UserInfo.getName();
cs.Written_date__c=system.Today();
cs.Role__c=ur.Name;

}
}
}
}

After adding the condition in red line then the code is not getting covered which is in brown,

test Method:-

-----------------------------------------------

@isTest
private class Updtcon {
static testMethod void Updtcon () {



Contact C= new contact(LastName='Anil');
insert c;

Opportunity o=new opportunity(name='Anil',StageName='prospecting',CloseDate=system.today(),Borrower__c=c.id);
insert o;

Finance__c f=new finance__c(Name='payment',opportunity__c=o.id,Contact__c=o.Borrower__r.id);
insert f;

Comments__c cm=new comments__c(Name='Raghu',Contact__c=c.id);
insert cm;

comments__c cms=new comments__c(Name='Anil',opportunity__c=o.id);
insert cms;

Comments__c cs=new comments__c(Name='Anil',Finance__c=f.id);
insert cs;

}
}

 

Thanks

Anil.B

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi Anil,

 

you can try the below test method code. Hope it helps!!

@isTest
private class Updtcon {
	static testMethod void Updtcon () {

	Id idRecordType = [select Id,name from RecordType where name='Applicant' and SObjectType='Contact' limit 1].Id;
	Contact C= new contact(LastName='Anil', RecordTypeid = idRecordType);
	insert c;

	Opportunity o=new opportunity(name='Anil',StageName='prospecting',CloseDate=system.today(),Borrower__c=c.id);
	insert o;

	Finance__c f=new finance__c(Name='payment',opportunity__c=o.id,Contact__c=o.Borrower__r.id);
	insert f;

	Comments__c cm=new comments__c(Name='Raghu',Contact__c=c.id);
	insert cm;

	comments__c cms=new comments__c(Name='Anil',opportunity__c=o.id);
	insert cms;

	Comments__c cs=new comments__c(Name='Anil',Finance__c=f.id);
	insert cs;

	}
}

 

regards

Sam

maja madi