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
Sean M 3Sean M 3 

Flow Redirect Apex Test

Good day developers

I have a Apex Class in Sandbox which redirects a user to a record created from a visual workflow with the help of a visualforce page:

public with sharing class FlowRedirectController {

    public Object FlowRedirectController() { 
        String unique_id = ApexPages.currentPage().getParameters().get('id');
        
        if(unique_id == null){
            // Return Home if no ID 
            String url = '/home/home.jsp';
            return new PageReference(url);
         } 
         
         // Get Order ID and set Redirect 
         String orderId = [SELECT Name,
                                    Unique_Flow_Identifier__c, 
                                    Id  
                             FROM Order  
                             WHERE Unique_Flow_Identifier__c = :unique_id 
                             ORDER BY CreatedDate DESC   
                             LIMIT 1].Id;
                             
        // Did we find a Order? 
        if (orderId == null) {
            // Return Home if no ID 
            String url = '/home/home.jsp'; 
            return new PageReference(url); 
            }

        // Redirect to Order 
        String url = '/' + orderId;
        return new PageReference(url); 
        }
}


When deploying to production I am getting code coverage error. This is becasue I need to create an Apex Test Class however being new to Apex i'm unsure what I would need to test in this code... Is anyone able to help with this?

Thanks
Best Answer chosen by Sean M 3
Abhishek BansalAbhishek Bansal
Hi Sean,

Yes, you are right. We need to write a test class for all the apex code being writte in our sandbox org before moving it to Production. In this particular case you need to create a dummy reference of your VF page and then test all the possible cases of your controller class so that it can be covered at least 75% to be eligible for Prod deployment.
I have written a test class for your controller class which may give you code coverage around 90%.
@isTest 
private class FlowRedirectControllerTest {
    static testMethod void testAllCases() {
       Unique_Flow_Identifier__c testUniqueFlowRecord = new Unique_Flow_Identifier__c();
	   testUniqueFlowRecord.Name = 'Test Unique Flow Record';
	   //Please enter all the required fields here
	   
	   insert testUniqueFlowRecord;
	   
	   //test with no parameters
	   FlowRedirectController testController = new FlowRedirectController();
	   
	   //test with incorrect id
	   PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', 'testincorrectId');
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController = new FlowRedirectController();
	   
	   //test with correct id
	   PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', testUniqueFlowRecord.id);
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController = new FlowRedirectController();
    }
}
Please take care of the comments mentioned in the above class. Let me know in case there is an issue.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Sean,

Yes, you are right. We need to write a test class for all the apex code being writte in our sandbox org before moving it to Production. In this particular case you need to create a dummy reference of your VF page and then test all the possible cases of your controller class so that it can be covered at least 75% to be eligible for Prod deployment.
I have written a test class for your controller class which may give you code coverage around 90%.
@isTest 
private class FlowRedirectControllerTest {
    static testMethod void testAllCases() {
       Unique_Flow_Identifier__c testUniqueFlowRecord = new Unique_Flow_Identifier__c();
	   testUniqueFlowRecord.Name = 'Test Unique Flow Record';
	   //Please enter all the required fields here
	   
	   insert testUniqueFlowRecord;
	   
	   //test with no parameters
	   FlowRedirectController testController = new FlowRedirectController();
	   
	   //test with incorrect id
	   PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', 'testincorrectId');
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController = new FlowRedirectController();
	   
	   //test with correct id
	   PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', testUniqueFlowRecord.id);
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController = new FlowRedirectController();
    }
}
Please take care of the comments mentioned in the above class. Let me know in case there is an issue.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Sean M 3Sean M 3
Hi Abhishek,

Thanks for your reply.

I have tried your code but not quite working - maybe a few things to note:

Unique_Flow_Identifier__c is not an object, it's a field on the Order object used to store the id variable of the created Order record.

The visual workflow creates an Order record and stores the id in Unique_Flow_Identifier__c. This field is then referenced in the Apex Controller to redirect the User to the newly created Order via VF page.

I've amended the code to compile but i'm getting no code coverage:
 
@isTest 
private class FlowRedirectControllerTest {
    static testMethod void testAllCases() {
       Order testUniqueFlowRecord = new Order();
       testUniqueFlowRecord.Name = 'Test Unique Flow Record';
       //Please enter all the required fields here
       testUniqueFlowRecord.AccountId='0018E00000sk6Q2QAI';
       testUniqueFlowRecord.EffectiveDate=date.today();
       testUniqueFlowRecord.Status='Draft';
      
       
       insert testUniqueFlowRecord;
       
       //test with no parameters
       FlowRedirectController testController = new FlowRedirectController();
       
       //test with incorrect id
       PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
       pageRef.getParameters().put('Id', 'testincorrectId');
       Test.setCurrentPageReference(pageRef);
       FlowRedirectController testController2 = new FlowRedirectController();
       
       //test with correct id
       pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
       pageRef.getParameters().put('Id', testUniqueFlowRecord.id);
       Test.setCurrentPageReference(pageRef);
       FlowRedirectController testController3 = new FlowRedirectController();
    }
}

Any ideas?
Abhishek BansalAbhishek Bansal
Hi Sean,

Yes, that was my mistake but you have corrected the code perfectly. In order to reflect the test covergare you have to click on the Run Test button that is present in this class detail page. After the test class is ran successfully, the code coverage will be reflected in your controller class.

Thanks,
Abhishek Bansal
Sales Five 1Sales Five 1
Dear Sean and  Abhishek, 

I'm using the same code for redirect and having the same issue with APEX test class. It's still show 0. Has your issue solved? Would you mind to share code with me?
Abhishek BansalAbhishek Bansal
Hi,

Test class code is already shared above. What else you are looking for?
Please share your controller class and test class so that we can figure out the issue.

Thanks,
Abhishek Bansal.
Sales Five 1Sales Five 1
Hi Abhishek, 

Below is my controller and test class. Unfornatly I still can't deploy to Production. It's showing an error that my code coverage is 0%. I did run a test.

Here is my controller
public with sharing class FlowRedirectController {

    public Object FlowRedirectController() { 
String unique_id = ApexPages.currentPage().getParameters().get('id');
    if(unique_id == null){
// Return Home if no ID 
String url = '/home/home.jsp'; return new PageReference(url); 
} 
// Get Contact ID and set Redirect 
String contactId = [SELECT Name,  Unique_Flow_Identifier__c, Id  FROM Opportunity  WHERE Unique_Flow_Identifier__c = :unique_id ORDER BY CreatedDate DESC LIMIT 1].Id;
// Did we find a Contact? 
if (contactId == null) {
// Return Home if no ID 
String url = '/home/home.jsp'; 
return new PageReference(url); }
// Redirect to Contact 
String url = '/' + contactId; return new PageReference(url); }}

And here is my test class.
@isTest 
private class FlowRedirectControllerTest {
    static testMethod void testAllCases() {
       Opportunity testUniqueFlowRecord = new Opportunity();
	   testUniqueFlowRecord.Name = 'Test Unique Flow Record';
	   //Please enter all the required fields here
	   testUniqueFlowRecord.AccountId ='0011t00000DCwBYAA1'; //LookUp field
       testUniqueFlowRecord.CloseDate = date.today();
       testUniqueFlowRecord.StageName = 'RFP in Progress';
       testUniqueFlowRecord.Status__c = 'Open';
	   
        insert testUniqueFlowRecord;
	   
       //test with no parameters
	   FlowRedirectController testController = new FlowRedirectController();
	   
	   //test with incorrect id
	   PageReference pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', 'testincorrectId');
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController2 = new FlowRedirectController();
	   
	   //test with correct id
	   pageRef = Page.FlowRedirect; //Please change the FlowRedirect with your original VF page name
	   pageRef.getParameters().put('Id', testUniqueFlowRecord.id);
	   Test.setCurrentPageReference(pageRef);
	   FlowRedirectController testController3 = new FlowRedirectController();

    }
}
Abhishek BansalAbhishek Bansal
Hi,

I have put a lot of comments in the test class code. Did you follow all these comments and make sure that everything is written correctly?

Thanks,
Abhishek Bansal.
Sales Five 1Sales Five 1
Hi, 

The code is working. The coverage is 0%. I have read and follow all of your comment and it still not working. This is i'm not sure why.User-added image
Abhishek BansalAbhishek Bansal

Hi,

You should use debug statments in your code to find out the root cause of the issue. Looking at the code, everything looks fine.

Thanks,
Abhishek Bansal.

Richard Ludwig 12Richard Ludwig 12
Hi Everyone,
I ran into the same issue with this custom controller, with only a few minor alterations. I use it for creating Quote records.
The code for the test class has the 'testController' defined, but it's not actually calling it. That leaves the controller code coverage at 0%.
I also added test start and test end which then covers all code lines in my controller.

Here is the updated test class:

@isTest
private class FlowRedirectControllerTest {
    static testMethod void testAllCases() {
       Quote testUniqueFlowRecord = new Quote();
       testUniqueFlowRecord.Name = 'Test Unique Flow Record';
       //Please enter all the required fields here
        
        insert testUniqueFlowRecord;
        
       //test with no parameters  - I removed this part as it threw errors during testing
       // FlowRedirectController testController = new FlowRedirectController();
        
       //test with incorrect id
       Test.startTest();
       PageReference pageRef = Page.QuoteFlowRedirect; //Please change the FlowRedirect with your original VF page name
       pageRef.getParameters().put('id', 'testincorrectId');
       Test.setCurrentPageReference(pageRef);
       FlowRedirectController testController = new FlowRedirectController();
       testController.FlowRedirectController(); 
       Test.stopTest();
        
       //test with correct id
       Test.startTest();
       PageReference pageRef = Page.QuoteFlowRedirect; //Please change the FlowRedirect with your original VF page name
       pageRef.getParameters().put('id', testUniqueFlowRecord.Id);
       Test.setCurrentPageReference(pageRef);
       FlowRedirectController testController = new FlowRedirectController();
       testController.FlowRedirectController(); 
       Test.stopTest(); 
 
    }
}

Hope it helps anyone!
Thanks,
Richard