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
Uday KUday K 

Pagereference issue

Hello All,

 

when i try to cover the below code in the test class. The only first if condition is getting covered and it is not checking or covering the second if condition even if i write another test method in the test class.


And if i remove the return null statement and run the test class, it is covering all the blocks. But the condition is not validating (i.e., not giving any page message) and bypassing the conditions.

 

Can anyone give me an immediate reply. Would be very helpful.

 

public pagereference insertPayInfo(){

             if(creobj.CC1_Apartment__c=='' || creobj.CC1_Apartment__c == null)
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input Postal Code'));
                return null;
            }   
            if(creobj.CC1_City_Town__c=='' || creobj.CC1_City_Town__c == null)
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input City or Town'));
                return null;
            }  

pagereference pgref=new pagereference('www.salesforce.com');
return pgref;

}

 

 

Thanks,

Uday

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

Please post your testmethod as well to tell you exactly what needs to be done.

 

As a quick fix, you can update your code as below

 

public pagereference insertPayInfo(){
	pagereference pgref=new pagereference('www.salesforce.com');
	if(creobj.CC1_Apartment__c=='' || creobj.CC1_Apartment__c == null){
		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input Postal Code'));
		pgref = null;
	}   
	if(creobj.CC1_City_Town__c=='' || creobj.CC1_City_Town__c == null)
	{
		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input City or Town'));
		pgref = null;
	}
	return pgref;
}

 

All Answers

Jerun JoseJerun Jose

Please post your testmethod as well to tell you exactly what needs to be done.

 

As a quick fix, you can update your code as below

 

public pagereference insertPayInfo(){
	pagereference pgref=new pagereference('www.salesforce.com');
	if(creobj.CC1_Apartment__c=='' || creobj.CC1_Apartment__c == null){
		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input Postal Code'));
		pgref = null;
	}   
	if(creobj.CC1_City_Town__c=='' || creobj.CC1_City_Town__c == null)
	{
		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Fatal,'Please Input City or Town'));
		pgref = null;
	}
	return pgref;
}

 

This was selected as the best answer
Uday KUday K

Hello Jerun Jose,

 

Thanks a lot. It worked fine and now my test class is getting coverage and class is also working fine.

:)