+ Start a Discussion
Ty WhitfieldTy Whitfield 

Making Lightning VisualForce and Controller available in Classic

I've created a VF page with controller in Lightning.  I found out that a department that hasn't migrated to Lightning yet also needs access.  However, when I go to add the VF page to the page layout in Classic, it isn't shown.  Is there a way to make it available in Classic.

Here is the VF page code
<apex:page lightningStylesheets="true" action="{!loadPage}" Controller="TrainingSubscriptionListController"  >
  <!-- Begin Default Content REMOVE THIS -->
  <apex:outputtext value="{!tmp}" escape="false" id="tblHTML"/>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

and here is the contoller code
public class TrainingSubscriptionListController {
    @AuraEnabled
    Public List<Contact> acctContacts {get;set;}
    public string tmp {get;set;}
    
    public void loadPage(){
        string AcctId = System.currentPageReference().getParameters().get('Id');
        acctContacts = [Select Id, Training_Subscription_User__c, Name, Email from Contact where AccountId= :AcctId and Training_Subscription_User__c = true];
        
           tmp =     '<table id="myTable"  style="width:100%; table-layout: auto;" cellspacing="0" cellpadding="10"><tr>' + 
                '<td style="padding:15px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Name</b></td>' + 
                // '<td style="width:50px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                //'</td>' +
                '<td style=" background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Email</b></td>' +
                
                '</tr>';
            
            integer i = 0;              
            while(i <  acctContacts.size())
            {
                tmp = tmp + '<tr><td style="padding:15px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Name + '</a></td><td style="padding:5px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Email  +  '</a></td></tr>';
                i++;
            }
         tmp = tmp + '</table>';
    }

}

Best Answer chosen by Ty Whitfield
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

You can use below code:
Contact cont = new Conatct();
        cont.Name ='Test';
        cont.Email ='khan@test.com';
        //Add other fields
        insert cont;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(cont);
        TrainingSubscriptionListController tsc = new TrainingSubscriptionListController(sc);
        tsc.loadPage();

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

I trust you are doing very well.

To embed a Visualforce page into page layout, the Visualforce page should be using the standard controller tag referencing the same entity that will be used in the page layout. You can use or add this Visualforce page by modifying the page layout.

Reference: https://help.salesforce.com/articleView?id=000005105&language=en_US&type=1

Try below code:

Visualforce:
<apex:page lightningStylesheets="true" action="{!loadPage}" standardController="Contact" extensions="TrainingSubscriptionListController"  >
  <!-- Begin Default Content REMOVE THIS -->
  <apex:outputtext value="{!tmp}" escape="false" id="tblHTML"/>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

Controller:
public class TrainingSubscriptionListController {
    @AuraEnabled
    Public List<Contact> acctContacts {get;set;}
    public string tmp {get;set;}
    
    public TrainingSubscriptionListController(ApexPages.StandardController controller){
    }

    public void loadPage(){
        string AcctId = System.currentPageReference().getParameters().get('Id');
        acctContacts = [Select Id, Training_Subscription_User__c, Name, Email from Contact where AccountId= :AcctId and Training_Subscription_User__c = true];
        
           tmp =     '<table id="myTable"  style="width:100%; table-layout: auto;" cellspacing="0" cellpadding="10"><tr>' + 
                '<td style="padding:15px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Name</b></td>' + 
                // '<td style="width:50px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                //'</td>' +
                '<td style=" background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Email</b></td>' +
                
                '</tr>';
            
            integer i = 0;              
            while(i <  acctContacts.size())
            {
                tmp = tmp + '<tr><td style="padding:15px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Name + '</a></td><td style="padding:5px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Email  +  '</a></td></tr>';
                i++;
            }
         tmp = tmp + '</table>';
    }

}

After this, this page should show in the Page Layout editor and can be embedded on a page layout for Contact.​


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
Change your code as below
public class TrainingSubscriptionListController {
    @AuraEnabled
    Public static List<Contact> acctContacts {get;set;}
    public string tmp {get;set;}
    
	public TrainingSubscriptionListController(){
		
	}
    public void loadPage(){
        string AcctId = System.currentPageReference().getParameters().get('Id');
        acctContacts = [Select Id, Training_Subscription_User__c, Name, Email from Contact where AccountId= :AcctId and Training_Subscription_User__c = true];
        
           tmp =     '<table id="myTable"  style="width:100%; table-layout: auto;" cellspacing="0" cellpadding="10"><tr>' + 
                '<td style="padding:15px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Name</b></td>' + 
                // '<td style="width:50px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                //'</td>' +
                '<td style=" background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Email</b></td>' +
                
                '</tr>';
            
            integer i = 0;              
            while(i <  acctContacts.size())
            {
                tmp = tmp + '<tr><td style="padding:15px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Name + '</a></td><td style="padding:5px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Email  +  '</a></td></tr>';
                i++;
            }
         tmp = tmp + '</table>';
    }

}


 
Ty WhitfieldTy Whitfield
Thanks Khan.  That worked.  

I now am wondering how that wll change my test class.  Previously I tested using 
TrainingSubscriptionListController tsc = new TrainingSubscriptionListController();
        tsc.loadPage();
        Test.stopTest();
        System.assert(!String.isEmpty(tsc.tmp));

Now I am receiving "Constructor not defined [TrainingSubscriptionListController].0"
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

You can use below code:
Contact cont = new Conatct();
        cont.Name ='Test';
        cont.Email ='khan@test.com';
        //Add other fields
        insert cont;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(cont);
        TrainingSubscriptionListController tsc = new TrainingSubscriptionListController(sc);
        tsc.loadPage();

Regards,
Khan Anas
This was selected as the best answer
Ty WhitfieldTy Whitfield
Once again, you are a life-saver.  thanks Khan