VISUALFORCE INTERVIEW QUESTIONS

<<<<<<<Salesforce Visualforce Interview questions>>>>>>

1. What is VisualForce in Salesforce?

VisualForce is a component-based framework for Force.com that includes tag-based markup language like HTML.

Every VisualForce tag consists of finely-grained user components like page sections, controllers, fields, etc.

The framework is based on the MVC paradigm where controllers can be generated automatically and integrated with the database. There is a special APEX programming language that helps you in writing your controllers or extensions with the aid of AJAX components.

2. What is visualforce controllers and what are they?

A Visualforce controller is a set of instructions that specify what happens when a user interacts with the components specified in associated Visualforce markup, such as when a user clicks a button or link.

There are two types of controllers-
1. Standard Controller
2. Custom Controller

3. What is included with each standard controller?

Data: the fields for the associated object record that are API accessible, including the related records (5 up/1 down).

Actions: save, delete, view, edit, cancel.

4. What is the diffrence between standard controller and custom controller?

StandardController: Which already have a Standard functionalities (save, edit, delete,cancel), and passing recordID through Addressbar we can display the Record On VF-Page.

CustomController: it means we are going to implement our Controller methods in a Class using {get;set;} methods.

5. What is the difference between custom controller and extension?

A custom controller is an Apex class that implements all of the logic for a page without leveraging a standard controller. Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

A controller extension is an Apex class that extends the functionality of a standard or custom controller. Use controller extensions when:
You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.
You want to add new actions.
You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.

6. What is Standard List Controller?

Standard List Controller allows to display list of records in visualvalfoce page. Standard list controller provides additional pagination actions (First, Lat, Next & Previous) compared to standard controller.

Syntax: <apex:page standardController=”Account” recordSetVar=”accounts”>

defining this controller is similar to standard controller. But need to add one more attribute recordSetVar

7. What is Custom Controller?

 Custom controller is an apex class that implements customized logic to visualfore pages.

We can override or create new functionality by using custom controllers.

Syntax: <apex:page controller=”Account” >

8. Can we use standardController & controller attributes at a time?

No, We cannot reference both attributes at a time.

See below syntax to under stand

<apex:page standardController=”ControllerName” extensions=”Class1, Class2,..”> Correct

<apex:page Controller=”MYControllerName” extensions=”Class1, Class2,..”> Correct syntax

<apex:page standardController=”MYControllerName” Controller=”MyController”> Wrong

9.What are the tasks of standard controller?

A standard controller’s main tasks are as follows:

  • Controlling Data: Standard controllers bring information and give it to the page, list, discourse or shape, all are referred to as perspectives. While it is the view’s task to deal with the introduction of its information, the controller accumulates the information from the view as entered or changed by a client, and passes it to the information demonstrated for handling.
  • Controlling Actions: Standard controllers react to the client or automatic orders started from a view. Such summons, generally referred to as Actions, may be related with handling the information accumulated from the view, or essentially to react to a navigational link.
  • Controlling Navigation: Standard controllers navigate the client to a particular view related to an activity. They likewise handle redirection to resulting views, in light of the particular result of different activities and information tasks.

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

System mode –
1. System mode is nothing but running apex code by ignoring user’s permissions. For example, logged in user does not have to create permission but he/she is able to create a record.
2. In system mode, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied to the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user.
3. In Salesforce, all apex code run in system mode. It ignores user’s permissions. The only exception is anonymous blocks like developer console and standard controllers. Even runAs() method doesn’t enforce user permissions or field-level permissions, it only enforces record sharing.

User mode –
1. User mode is nothing but running apex code by respecting user’s permissions and sharing of records. For example, logged in user does not have to create permission and so he/she is not able to create a record.
2. In Salesforce, only standard controllers and anonymous blocks like developer console run in user mode.

ComponentDefault Mode
Apex ClassSystem Mode
Apex TriggerSystem Mode
standardControllerUser Mode
controllerSystem Mode
Extensions (standardController)User Mode
Extensions (controller)System Mode
Anonymous BlockUser Mode

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

On apex class we need a following code line-
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,’This is error message’));

we have following 5 severity levels available in salesforce-
1. CONFIRM
2. ERROR
3. FATAL
4. INFO
5. WARNING

And on Visualforce page we need a following tag-
apex:pageMessages or apex:pageMessage

12. Following classes are there what will be the priority –
controller=”MainClass”extensions=”ClassA,ClassB,ClassC”

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 extensionsin 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.

13. What is View State?

  1. Creation: In a visualforce page, whenever we use form tag then view state will be created for the page.
  2. 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, which ever 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.
  3. 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.
  4. Governor Limits: Maximum size of the View State is: 135 KB.
  5. 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.
  6. Static variables also won’t occupy space in the view state.
  7. 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.

14. 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’)

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

Please find the following differences –

Static ResourcesDocuments
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.

16. What is RecordSetVar?

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

17. 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.

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

1000

19. 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 will be increased from 1000 to 10000

20. What is the difference between inputText and inputField?

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

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

21. 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.

22. 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 –

  1. Using standardSetController
  2. Using Limit and Offset keywords in SOQL query.

23. What is Wrapper Class?

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

24. What are the custom labels?

Custom Label is a memory location where we can store the text.

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.

25. What are the Custom Settings?

There are two types of custom settigns –

  1. List Custom Settings
  2. Hierarchy Custom Settings

List custom settings –

  1. List Custom Settings are like custom objects.
  2. We can create the fields and we can store the records.
  3. List Custom Settings records will store in Application Cache memory.
  4. To access List Custom Settings records no need to use SOQL query.
  5. 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 –

  1. We can add users and profiles.
  2. After that we can check that a particular/current logged in user is part of this custom settings.
  3. We can refer this in below components –
    1. Validation Rules
    2. Apex Classes and Apex Triggers
    3. Visualforce Pages

26. What is the difference between List Custom Settings and Custom Object?

Please find the below differences –

List Custom SettingsCustom Objects
Stores data in Application Cache memory.Stores data in database.
No need to use SOQL query to fetch the records.We 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.

27. 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.

28. Compare and contrast custom controllers and controller extensions. How are they the same? How are they different?

Both allow for custom code to be used, allowing for custom data sets and custom actions. Extensions leverage the existing data and actions within a standard or custom controller. Custom controllers must contain all data and actions that need to be executed by the page. Extensions that extend standard controller allow for the pages which use those extensions to be used in custom buttons, standard button overrides, and over declarative features.

29. Is it always necessary to know apex to create visualforce pages? When does it become necessary?

No, it is not always necessary. You can use standard controllers and VF component tags to accomplish quite a bit.

Apex becomes necessary when you need either a custom set of data or custom actions to be available from the page.

30. What is the main difference between using Data Table Vs. Page Block Table tags?

Page Block Table: Display the records with standard salesforce look and feel.

Data Table: Display the records without standard salesforce look and feel.

31. What are some examples of javascript events?

Onmouseover, onclick etc.

32. What is button overriding?

We can override few standard buttons with visualforce pages.

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

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

33. 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.

34. 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.

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

Parameters Example: /apex/SamplePage?param1=val1&param2=val2

  1. ?param1=val1 : parameters should start with ? symbol.
  2. &param2=val2 : To add multiple parameters, each parameter should be separated with & symbol.

Ways of passing parameters –

  1. From a VF Page: Whenever we click on commandButtton or commandLink or outputLink, we can pass parameters with param tag. See the below example –
    click
  2. 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’);
  3. 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.
  4. 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.

36. 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 overriden 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.

37. Which tag is used with both radio buttons and picklists to create the selectable values?

apex:selectoption

38. What are the different ajax action tags? What does each do?

ActionStatus: used to display start and stop statuses of AJAX requests.

ActionSupport: used to call a second component when an event happens to the first component.

ActionPoller: similar to actionSupport, but the event is based on a timer instead of a user action.

ActionFunction: provides support for invoking a controller action from JavaScript code using an AJAX request by defining a new JavaScript function.

ActionRegion: used to demarcate which parts of the page the server should reprocess.

39.  Is there any alternative for the “ActionPoller”?

Using SetTimeout in Javascript and calling the apex method using apex:actionFunction

40. What is the difference between apex:pageMessages, apex:pageMessage, apex:Message and apex:Messages?

apex:PageMessages:
This component displays all messages that were generated for all components on the current page, presented using the salesforce styling. This will display both salesforce generated messages as well as custom messages added to the ApexPages class

apex:PageMessage:
Apex:PageMessage is a component that adds single message on the page. This is used to display custom message using the salesforce formatting

apex:Message:
apex:Message is used to display an error on only a specific field. It is used to allow developers to place field specific errors in specific location.

apex:Messages:
apex:Messages is similar to apex:Message but it displays all errors

41. How to reduce view state?

1. Add transient keyword before variables, like lists used just for printing the tables.
2. Try recreating state, by querying again the database, storing in local lists, sets or maps will not work.
3. Declare some variable as static if possible, create static code block to recreate them in each request.
4. Check your SOQL queries, if you are querying additional fields, never used or required on visualforce page.

42. Why we used transient key word?

The transient key word prevents the data from being saved into the view state. This should be used for very temporary variables.

43. What is remote action?

Remote action function in salesforce allows user to access any method from any class through javasrcipt methods, and get the result as a javascript object for further manipulation. Points to remember while implementing remote action function: Remote action method should have @RemoteAction annotation.

44. What is the need of “Custom Controller” in Visualforce as everything can be done by the combination of Standard Controller + Extension class.

  • Sharing setting is applied on standard object/extension by default; In case we don’t want to apply sharing setting in our code then Custom controller is only option.
  • It is possible that the functionality of page does not required any Standard object or may require more than one standard object, then in that case Custom controller is required.

45. In class declaration if we don’t write keyword “with sharing” then it runs in system mode then why keyword “without sharing” is introduced in apex?

Lets take example, there is classA declared using “with sharing” and it calls classB method. classB is not declared with any keyword then by default “with sharing” will be applied to that class because originating call is done through classA. To avoid this we have to explicitly define classB with keyword “without sharing”.

46. Consider a scenario where you have created a Visualforce page and Controller. You want to restrict the controller action for users which are logged in using “Grant Login Access”. How to acheive this?

When System admin logged in on the behalf of any other user. On upper right corner message is displayed that user is logged-in on behalf of some other user. In Visualforce page we can search for the element with class name present or not? If the element with that Class name exist means logged-in user is not a actual user.

47. When you get the error “Non-selective query against large object type”? how to resolve it?

Whenever an object has greater than 100K records any query on that object must be “selective”. For a query to be selective it must have enough indexed filters (where clauses) so that less than 10% of the records (in our example 10K) are returned before applying the limit statement.

48. How do you export data from Visualpage into excel and PDF?

We can export data into excel and PDF by using below content inside

<apex:page>

EXCEL:contentType=”application/vnd.ms-excel#SalesForceExport.xls” PDF: renderAs=”pdf”