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
Salah Odeh 10Salah Odeh 10 

How to create a custom controller and test for a visualforce page. I have created my visualforce page and need help with controller and test class

Here is my visualforce page:
​​​​​​<apex:page standardController="Bid_Summaries__c">
    <apex:pageBlock title="Bid Summary">
    <apex:slds />
    <style type="text/css">
        body {background-color: white;}
        p { font-weight: bold; }
        p { font-style: italic;}
        h1 { color: #f00; }
        p { background-color: #eec; }
        newLink { color: #f60; font-weight: bold; }   
    </style>
            <apex:page lightningStylesheets="true"/>  
            <apex:form >       
            <apex:pageBlockTable value="{! Bid_Summaries__c }" var="Bid_Summaries__c">
            <apex:column value="{! Bid_Summaries__c.Name }"/>                  
            <apex:column value="{! Bid_Summaries__c.Original_Budget__c }"/>
            <apex:column value="{! Bid_Summaries__c.Upgrade_Amount__c }"/>
            <apex:column value="{! Bid_Summaries__c.Budget_incl_Upgrades__c }"/>
            <apex:column value="{! Bid_Summaries__c.TJH_Service_Fee__c }"/>
            <apex:column value="{! Bid_Summaries__c.Concession__c }"/>
            <apex:column value="{! Bid_Summaries__c.Upgrade_Total__c}"/>
            </apex:pageBlockTable>
            </apex:form>
      </apex:pageBlock>
</apex:page>

What I'm trying to do is dynamically populate the value of the Bid_Summaries__c object fields on a visualforce page where any changes to the fields in that object will reflect in realtime to the visualforce page. The Bid_Summaries__c object is showing on a record list on the community as Kanaban list, users are able to move the field to select options and change prices. The point is once a user change those fields it will reflect on the visualforce page dynamicly without the need to refresh the page. Any ideas on how to write the apex controller and test class, or if there is any other way to accomplish this. Thanks
Rounak SharmaRounak Sharma

hi Salah Odeh ,
Please visit the below url it is similar to what you are looking for.

https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals/visualforce_custom_controllers

Please mark it as a best answer if it helps you.

Salah Odeh 10Salah Odeh 10
Hi Rounak,

Thanks for your respones, I have already built a apex controller and a test class, but when I run the test there is no coverage. Not sure what I'm missing. Here is my controller and test class:

Controller: 

​​​​​​public class BidSummariesController {
    
        
        public List<Bid_Summaries__c> Bid {get; set;}
        public BidSummariesController(){
        List<Bid_Summaries__c> Bid= [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c] ;
    }
                       PageReference pageRef = Page.BidSummary1;                    
                       public list <Bid_Summaries__c> getBid(){
                                              
                       return Bid;
                          
        }
    
    }

Test Class:

@isTest 

    public class BidSummariesControllertest {
        
        public static testMethod void BidSummariesController() {
            
         test.StartTest();             
         Bid_Summaries__c Bid = new Bid_Summaries__c();
         
         Bid.Upgrade_Amount__c = 1000;
         Bid.Concession__c = 1000;         
         Bid.Original_Budget__c = 100;
         Bid.Status__c    = 'New';
            
            Insert Bid;
           
Rounak SharmaRounak Sharma

hi salah,
You need to call the controller in your test class.

ApexPages.StandardController sc = new ApexPages.StandardController(Bid);
   PageReference pageRef = Page.PageName;

Please mark it as a best answer if it helps you.

Rounak SharmaRounak Sharma

one more thing is you need to add test.stopTest also at the end.

also add Bid_Summaries__c bs = new Bid_Summaries__c(sc);

Salah Odeh 10Salah Odeh 10
Hi Rounak,

I actually did call the controller but did not copy it here. Here is the test class:

@isTest 

    public class BidSummariesControllertest {
        
        public static testMethod void BidSummariesController() {
            
         test.StartTest();             
         Bid_Summaries__c Bid = new Bid_Summaries__c();
         
         Bid.Upgrade_Amount__c = 1000;
         Bid.Concession__c = 1000;         
         Bid.Original_Budget__c = 100;
         Bid.Status__c    = 'New';
            
            Insert Bid;
           
            
        PageReference pageRef = Page.BidSummary1;
        Test.setCurrentPage(pageRef);
        ApexPages.StandardController sc = new ApexPages.StandardController(Bid);
            test.stopTest();
        
        
        List<Bid_Summaries__c> BidSum = [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c ];
        
         
   
                } 
        
        List<Bid_Summaries__c> BidSumm = New List<Bid_Summaries__c>();
             
        
        
              
                                      
        }
Salah Odeh 10Salah Odeh 10
Still the test does not give any coverage. This code :add Bid_Summaries__c bs = new Bid_Summaries__c(sc) is giving me an error
Rounak SharmaRounak Sharma
can you please let me see the error and exctly which line is not covered
what error you are getting?
 
Salah Odeh 10Salah Odeh 10
The test class has 0 coverage to the controller and here is the error:

Type requires name=value pair construction: Bid_Summaries___c