Tag Archives: StandardController

Get Data From Visualforce Controller Extension Without SOQL Query

When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. But we can use StandardController.addFields(List fieldNames) method, which adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

public with sharing class AccountControllerExt {
    public Account acc {get; set;}
    
    public AccountControllerExt(ApexPages.StandardController controller) {
        List<String> accFieldList = new List<String>();
        //Passing a list of field names to the standard controller
        controller.addFields(new List<String>{'Id', 'Name', 'AccountNumber', 'Website', 'Phone','Type', 'Industry'});
        //Standard controller to retrieve the field data of the record
        acc = (Account)controller.getRecord();
    }
}

Add Fields in ApexPages.StandardController

If you want any fields value then you need add the field API name in standard controller.

public with sharing class AccountControllerExt
{
	public Account acc {get;set;}
	 
	public AccountControllerExt(ApexPages.StandardController stdController)
	{
		stdController.addFields(new List<String>{'Name', 'Region__c', 'TAX_ID_Number__c'});
		this.acc = (Account)stdController.getRecord();
	}
}

Salesforce StandardController Methods

The following methods are for StandardController:

addFields(fieldNames)
When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

cancel()
Returns the PageReference of the cancel page.

delete()
Deletes record and returns the PageReference of the delete page.

edit()
Returns the PageReference of the standard edit page.

getId()
Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

getRecord()
Returns the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

reset()
Forces the controller to reacquire access to newly referenced fields. Any changes made to the record prior to this method call are discarded.

save()
Saves changes and returns the updated PageReference.

view()
Returns the PageReference object of the standard detail page.

Salesforce StandardSetController Methods

The following methods are for StandardSetController:

cancel()
Returns the PageReference of the original page, if known, or the home page.

first()
Returns the first page of records.

getCompleteResult()
Indicates whether there are more records in the set than the maximum record limit. If this is false, there are more records than you can process using the list controller. The maximum record limit is 10,000 records.

getFilterId()
Returns the ID of the filter that is currently in context.

getHasNext()
Indicates whether there are more records after the current page set.

getHasPrevious()
Indicates whether there are more records before the current page set.

getListViewOptions()
Returns a list of the listviews available to the current user.

getPageNumber()
Returns the page number of the current page set. Note that the first page returns 1.

getPageSize()
Returns the number of records included in each page set.

getRecord()
Returns the sObject that represents the changes to the selected records. This retrieves the prototype object contained within the class, and is used for performing mass updates.

getRecords()
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call clear() on it.

getResultSize()
Returns the number of records in the set.

getSelected()
Returns the list of sObjects that have been selected.

last()
Returns the last page of records.

next()
Returns the next page of records.

previous()
Returns the previous page of records.

save()
Inserts new records or updates existing records that have been changed. After this operation is finished, it returns a PageReference to the original page, if known, or the home page.

setFilterID(filterId)
Sets the filter ID of the controller.

setpageNumber(pageNumber)
Sets the page number.

setPageSize(pageSize)
Sets the number of records in each page set.

setSelected(selectedRecords)
Set the selected records.

Get the List of Visualforce Pages defined with the standardController

To get the list of Visualforce Page which are defined with the standardController attribute, we can write SOQL query on ApexPage object with passing ControllerType field value Standard.

Sample SOQL:

SELECT Name, ApiVersion, ControllerKey, ControllerType, Description, NamespacePrefix, MasterLabel, CreatedDate FROM ApexPage Where ControllerType = 'Standard'