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
adeetz54724adeetz54724 

How to write test class for pageReference using external ID in URL

I am trying to get code coverage for the class below:
public class redirectToWebinarPageController{
    public pageReference redirectURL(){
        
        //get the external id from the URL
        String externalId = ApexPages.currentPage().getParameters().get('externalId');
        
        //query the record.
        Webinar__c webinarRecord = [select id from Webinar__c where Webinar_Count__c =: externalId];
        
        //redirect to detail page of the above record.
        return new pageReference('/'+webinarRecord.id);
       
    }
}
Here is the test I have and am getting 75% coverage but I would prefer to know how to get 100%
@isTest 
public class redirectToWebinarPageControllerTest {
    public static testMethod void testredirectURL() {
        test.startTest();
        PageReference pageRef = Page.redirectToWebinarDetailPage;
        
        //Your test data logics
        Account testAccount = new Account();
        testAccount.Name = 'Account Name';
        insert testAccount;
        
        Contact testContact = new Contact();
        testContact.LastName = 'Contact';
        testContact.FirstName = 'Test';
        testContact.Email = 'testContact@testing.com';
        insert testContact;
        
        Webinar__c w = new Webinar__c();
        w.Name = 'Test Webinar';
        w.Account__c = testAccount.id;
        w.Communication_Type__c = 'Client 4';
        w.Delivery_Method__c = 'Self Study';
        w.Contact__c = testContact.id;
        insert w;
        
        String testExternalId = w.Webinar_Count__c;

        Test.setCurrentPage(pageRef);
			
			pageRef.getParameters().put('externalId',testExternalId);
			
			redirectToWebinarPageController controller = new redirectToWebinarPageController();
			controller.redirectURL();
        
   		test.stopTest();
     }
}
Thank you in advance, I am really new to writing test classes and would love to know what I am missing here.
Best Answer chosen by adeetz54724
Maharajan CMaharajan C
Hi Angie,

1.  I am not seeing any input value for Webinar_Count__c field in test class. So i guess it will be automatically generated by trigger/some automation.If this statement is correct then use query to get the Webinar_Count__c from db.

Webinar__c w = new Webinar__c();
w.Name = 'Test Webinar';
w.Account__c = testAccount.id;
w.Communication_Type__c = 'Client 4';
w.Delivery_Method__c = 'Self Study';
w.Contact__c = testContact.id;
insert w;

Webinar__c webRec = [select id,Webinar_Count__c from Webinar__c where Id =: w.Id limit 1]; 

String testExternalId = webRec.Webinar_Count__c;

2.  If  Webinar_Count__c needs manual input then set the value in test class.
 
w.Webinar_Count__c = '12345';
insert w;

3.  use the ApexPages.CurrentPage() instead of pageRef.getParameters() in test class:
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('externalId',testExternalId); 
redirectToWebinarPageController controller = new redirectToWebinarPageController();
controller.redirectURL();

Thanks,
Maharajan.C

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Angie,

Greetings!

I would suggest you to check which lines are covered and which are not covered makes easy for your to improve the code coverage.

How to check:Go to Setup>Open Developer console>Open the Apex class and Test class in two tabs
>Please run the test class in Developer console
>Go back to the apex class of the test class.
>Click on the arrow besides the Code coverage which shows the test class name with the percentage and the uncovered lines will be in red color.

In this way,you can work on the uncovered code to make sure you have 100% code coverage and aslo,it is not neccessary that you have 100% code coverage or every class in salesforce.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
adeetz54724adeetz54724
@shirisha All is covered except last line and I have been trying to figure out how to cover it for over a week, I am not having any luck.
Maharajan CMaharajan C
Hi Angie,

1.  I am not seeing any input value for Webinar_Count__c field in test class. So i guess it will be automatically generated by trigger/some automation.If this statement is correct then use query to get the Webinar_Count__c from db.

Webinar__c w = new Webinar__c();
w.Name = 'Test Webinar';
w.Account__c = testAccount.id;
w.Communication_Type__c = 'Client 4';
w.Delivery_Method__c = 'Self Study';
w.Contact__c = testContact.id;
insert w;

Webinar__c webRec = [select id,Webinar_Count__c from Webinar__c where Id =: w.Id limit 1]; 

String testExternalId = webRec.Webinar_Count__c;

2.  If  Webinar_Count__c needs manual input then set the value in test class.
 
w.Webinar_Count__c = '12345';
insert w;

3.  use the ApexPages.CurrentPage() instead of pageRef.getParameters() in test class:
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('externalId',testExternalId); 
redirectToWebinarPageController controller = new redirectToWebinarPageController();
controller.redirectURL();

Thanks,
Maharajan.C
This was selected as the best answer
adeetz54724adeetz54724
Thank you so much! I had tried the query and left off something because it would not work. I have marked this as best answer.