Back

Visualforce Basics Q&A

1. Visualforce Basics.

Previously Visualforce Pages were not there, instead S-Controls were there. S-controls have been superseded by Visualforce pages. Organizations that haven’t previously used s-controls can’t create them. Existing s-controls are unaffected, and can still be edited.

S-controls: It is a combination of HTML and S-control but with this to achieve saelsforce look and feel, it is very difficult and consume lot of tags. Visualforce: With visualforce we can easily achieve the salesforce look and feel by consuming very few tags.

Browsers (IE, Chrome, Firefox, etc…) can understand only HTML, Visualforce will be converted to HTML before display on the browser by salesforce.

2. What is the difference between system mode (system context) and user mode (user context)?

System Mode: Current logged in user permissions (Object-level, Field-level, Record-level security) won’t be considered. Even though user doesn’t has access, it will provide full permissions. User Mode: Current logged in user permissions (Object-level, Field-level, Record-level security) will be considered.

See the following table –

Component – Default Mode

Apex Class – System Mode

Apex Trigger – System Mode

standardController – User Mode

controller – System Mode

Extensions (standardController) – User Mode

Extensions (controller) – System Mode

Anonymous Block – User Mode

Note: to apply security for Apex Class, we can use with sharing while declaring the class. With sharing will apply only Record-level security. It won’t apply Object-level and Field-level security.

3. What are the types of controllers available for the visualforce page?

There are the two types of controllers –

  • Standard Controller
  • Custom Controller

Note: On a visualforce page, we can use either standardController (attribute name for Standard Controller) or controller (attribute name for Custom Controller). We cannot refer both at a time.

StandardController –

We can refer any standard object or custom object in this attribute. At a time we can refer only one object and we cannot refer apex class in this attribute. standardController by default works in user mode.

Custom Controller –

We can refer any apex class in this attribute. At a time we can refer only one apex class in this attribute. Controller by default works in system mode. Other supported attributes for the controllers –

Extensions

Standard List Controller

Custom List Controller

Extensions –

We can refer this attribute along with standardController or controller attribute. In extensions attribute, we can refer multiple apex classes. We cannot refer standard/custom objects.

Standard List Controller –

There is no such attribute called standard list controller but in the standardController page if you mention recordsetVar attribute then that page we can call it as standard list controller page.

In recordsetVar attribute we can give any name which will hold the list of records of the object which we mentioned for the standardController.

Custom List Controller –

There is no such attribute called custom list controller but in the controller page if we display multiple records then that page we can call it as custom list controller. In controller class, we have to query the records from the database to display the records on the page.

4. Following classes are there what will be the priority – controller=”MainClass” extensions=”ClassA,ClassB,ClassC” click to see the question in detail. A method with the name save exists in all the classes mentioned above. Which will execute first and what is the order?

First of all it will give priority for the extensions, in case there are multiple classes referred then it will give priority from left to right after that only it will check in main controller (either standardController or controller).

In the above scenario, it will invoke the save method from ClassA.

5. What is View State?

Creation: In a visualforce page, whenever we use form tag then view state will be created for the page.

Purpose: Assume that, we need to display the user input form in 3 different visualforce pages. In first and second pages, user has to fill the information and click on Next button. On the final page, after filling the form if he/she will click on save button, whichever the information we filled in the first and second pages should be also saved. View state will store the information which is filled in first and second page. To maintain the state of the page, we need view state.

Size: All the variables which we use in controller/extensions classes and expressions which are declared on the page will occupy the space in view state. Governor Limits: Maximum size of the View State is: 135 KB.

Page Performance: Whatever the variables information we don’t required to maintain the state while navigating to other pages those variables we can decorate with transient keyword which won’t occupy space in the view state.

  • Static variables also won’t occupy space in the view state.

It is recommended to use only one form tag. If there are multiple form tags hierarchy of the folder structure increases which will occupy more space.

6. What are the Static Resources?

To store the following kind of files and refer on the visualforce page –

  • images
  • javaScript/jQuery
  • CSS Style Sheets
  • zip files

Overall size of the static resources per organization: 250 MB

Size of the each static resource file: 5 MB

To refer the static resource on the VF page use: $Resource.FileName

To refer the zipped static resource on the VF page use: URLFOR($Resource.zipname, ‘/images/FileName.png’)

7. What is the defference between Static Resources and Documents?

Please find the following differences – Static Resources Documents Static Resource is cached at server side Documents will store in database Static Resources can be referred with file name Documents should be referred with url

8. How to display error messages on VF (Visualforce) page?

On VF page we should use –

<apex:pageMessages ></apex:pageMessages>

In Apex Class, we should use –

ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,’Error Message’));

Note: For the standardController no need to include code inside of the Apex Class.

Following severity levels are available –

  • CONFIRM
  • ERROR
  • FATAL
  • INFO
  • WARNING
9. What is RecordSetVar?

On a standardController page to display the multiple records we use recordSetVar (without the support of any extensions class).

10. What is the difference between dataTable and pageBlockTable?

dataTable: Display the records without standard salesforce look and feel. pageBlockTable: Display the records with standard salesforce look and feel.

11. Maximum number of records displayed on the VF page?

1000

12. How to display more records beyond the supported limit on the VF page?

For the page tag we can enable readOnly attribute value as true so that – Number of query rows will increased from 50000 to 1 million rows. Number of records displayed on VF page can be increased from 1000 to 10000

13. What is the difference between inputText and inputField?

inputText: Always display the field as text box irrespective of data types (Checkbox, Picklsit, Look up).

inputField: Display automatically according to the fields data types. Note: We cannot refer primitive data types (String etc.) with inputField.

14. What is the difference between outputText and outputField?

outputText: Always display the field value as text irrespective of data types (Currency, Look up, URL etc.)

outputField: Display automatically according to the fields data types. If it is currency then it will append currency symbol while displaying on the page. If it is look up type then it will display as a link.

15. What is pagination and what are the ways to achieve it?

Assume that we need to display 100 records on the page.

If the requirement is to display only 10 records at a time –

First: Displays first set of 10 records.

Previous: Displays previous set of 10 records.

Next: Displays next set of 10 records.

Last: Displays last set of 10 records.

We can achieve the above functionality with Pagination. We can achieve the pagination in two ways –

  • Using standardSetController
  • Using Limit and Offset keywords in SOQL query
16. What is Wrapper Class?

Wrapper Class is nothing but list of instances of a certain class.

Follow the below steps to create the Wrapper list –

Create an Apex Class say ‘MyWrapper'(It can be inside/outside of the controller class.)

Declare the necessary variables in the class to store the information.

Create a list called ‘MyWrapList’ for ‘MyWrapper’.

Create multiple instances for‘MyWrapper’ and store it in ‘MyWrapList’ list.

Access ‘MyWrapList’ list from VF page.

In the below scenarios, we have to go for wrapper class –

If you want to display checkboxes along with records so that upon selecting checkboxes corresponding records can be processed (Updating, Deleting, etc.). If you need to display records in single table by combining the columns which belongs to multiple objects

17. What are the custom labels? Custom Label is a memory location where we can store the text. Where to use?
  • We can refer in below components –
  • Validation Rules and Formula fields
  • Apex Classes and Apex Triggers
  • Visualforce Pages

What is the advantage? Assume that you are referring a Record Type Name multiple times in an apex class; in future if the client asked to rename the record type then in all the palaces we need change by searching which consume good amount of time.

To avoid that if you store the Record Type Name in custom label and refer that custom label in all the places.

If you need to change the name in future then you need to change only in that custom label.

Assume that you are displaying an error message on the visualforce page; in future if the client asked to change the error message then it will be a code change.

For code change it requires lot of approvals from business which consume lot of time. If you keep the error message in a custom label and refer on the page, in future it won’t be a code change, in production directly they can replace the error message in that custom label.

18. What are the Custom Settings?

There are two types of custom settings –

  • List Custom Settings
  • Hierarchy Custom Settings
  • List custom settings:
  • List Custom Settings are like custom objects.

We can create the fields and we can store the records.

List Custom Settings records will store in Application Cache memory.

To access List Custom Settings records no need to use SOQL query.

Example: Based on the regions, zip codes should be retrieved. In this case, If you store these records in a custom object every time we need to query from the database. Instead if you store the records in List Custom Settings, without consuming SOQL query we can capture the information from the database.

Hierarchy custom settings:

We can add users and profiles.

After that we can check that a particular/current logged in user is part of this custom settings.

We can refer this in below components –

  • Validation Rules
  • Apex Classes and Apex Triggers
  • Visualforce Pages
19. What is the difference between List Custom Settings and Custom Object?

Custom Settings Custom Objects

  • Stores data in Application Cache memory Stores data in database
  • No need to use SOQL query to fetch the records Need to use SOQL query to fetch the records from the database
  • Limited data types are available when compared to Custom Object All the data types will be available
  • We cannot create validation rules and apex triggers on List Custom Settings We can create
  • We cannot create tab for List Custom Settings We can create
20.What is the difference between rendered, rerender, renderAs and contentType?

rendered: Accepts true or false. If it is true then component will display on the page, if it is false then it won’t display on the page.

reRender: To refresh certain area of a page based on component id.

renderAs: Used in page tag, We can display VF page in PDF format if we give renderAs = “PDF”.

contentType: Used in page tag, we can download VF page in MS Word/Excel etc. based on the input to contentType.

21.What is button overriding?

We can override the behavior of standard buttons — like New, View, or Edit — in Salesforce Classic, Lightning Experience, and mobile independently.

We can override few standard buttons with visualforce pages.

To override standard button with visualforce page, VF page should use standardController to an object which is related to standard button.

Custom buttons we can override with URL, javaScript or Visualforce.

22. What are the inline visualforce pages?

On a record detail page we can embed visualforce pages. Assume that you are displaying inline VF page on Account record detail page. VF page should be standardCotnroller to Account Object.

Note: We cannot embed VF page inside of the Edit page.

23. What is the use of immediate attribute?

Whenever we click on Back or Cancel button on a VF page if there are mandatory fields then we will see the error messages saying to populate those field values. To bypass validations upon clicking on a button/link, we can use immediate attribute.

24. What are the parameters and how many ways we can pass the parameters?

Parameters Example: /apex/SamplePage?param1=val1¶m2=val2

?param1=val1 – parameters should start with ? symbol

¶m2=val2 – To add multiple parameters, each parameter should be separated with & symbol.

Ways of passing parameters –

From a VF Page: Whenever we click on commandButtton or commandLink or outputLink, we can pass parameters with param tag.

See the below example –

<apex:commandButton action=”{!Increment}” value=”Increment” reRender=”result”>
<apex:param assignTo=”{!amount}” value=”10″/>
</apex:commandButton>

From pageReference: Upon calling an action method while returning pageReference we can append parameters.

See the below example –

PageReference nextpage = new PageReference(‘/apex/SamplePage’);
nextpage.getParameters().put(‘param1′,’val1’);
nextpage.getParameters().put(‘param2′,’val2’);
  • Assume that there are three VF pages which are using the same Apex Class. In this case no need to pass the parameters from one page to other to hold the information.
  • Assume that there are three VF pages which are using three different Apex Classes. In this case to hold information from one page to other, we should pass the parameters.
25.What is retURL and saveURL?
  • We cannot override Save button. When you are on record edit page upon clicking on Save button if you want to navigate it to a specific URL, then we need to append saveURL=someURL parameter to the URL.
  • You will come to this edit page if you click on ‘New’ or ‘Edit’ button which can be overridden with VF page, from this page you can pass saveURL parameter to edit page.
  • When you are on record edit page upon clicking on Cancel button if you want to navigate it to a specific URL, then we need to append retURL=someURL parameter to the URL.
26. What are the Visualforce Components?

Salesforce provides a library of standard, pre-built components, such as <apex:relatedList> and <apex:dataTable>, that can be used to develop Visualforce pages. In addition, you can build your own custom components to augment this library. Similar to the way you can encapsulate a piece of code in a method and then reuse that method several times in a program, you can encapsulate a common design pattern in a custom component and then reuse that component several times in one or more Visualforce pages.

<apex:component>
<apex:attribute name=”myattribute” type=”String” description=”TODO: Describe me”/>
<!– Begin Default Content REMOVE THIS –>
<h1>Congratulations</h1>
This is your new Component: mynewcomponent
<!– End Default Content REMOVE THIS –>
</apex:component>
27. When to use VF Component?

If you want to reuse the VF page logic, then we can use VF Components. If the VF page logic is huge and if you want to split into different pieces, then we can use VF Components.

28. What is the order of execution of VF page?

We can request VF page in two ways –

Get Request: Whenever we click on a link or button or directly hitting the url in address bar we can open a VF page. Get Request Order

Postback Request: On a VF page after populating fields if you click on save button certain action will invoke it is nothing but postback request. Postback Request Order

29. What are the types of Ajax Functions in VF page and what is the usage?

There are around 5 AJAX functions –

  • actionSupport – this component is used to invoke actions like Javascript, HTML input fields to a controller used for visualforce pages.
  • actionStatus – this component is used to notify the status of an asynchronous actions and can also be used to notify when the action starts and when the action stopped..
  • actionFunction – this component is used to call method in a controller as a JavaScript function and can also be used anywhere in the visualforce page to call action which include custom JavaScript code.
  • actionPoller – this component refreshes the connection regularly, keeping login sessions alive.
  • actionRegion – an area of a Visualforce page that demarcates which components should be processed by the Force.com server when an AJAX request is generated. Only the components in the body of the <apex:actionRegion> are processed by the server, thereby increasing the performance of the page.
Back
Site developed by Nikhil Karkra © 2021 | Icons made by Freepik