Thursday, July 20, 2017

Sales force interview questions:Visual force pages

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 java script 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.
Visualforce page should start with the following syntax -
?
1
2
3
<apex:page>
 <!-- You can HTML code here along with using apex tags -->
</apex:page>
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 -
ComponentDefault Mode
Apex ClassSystem Mode
Apex TriggerSystem Mode
standardControllerUser Mode
controllerSystem Mode
Extensions (standardController)User Mode
Extensions (controller)System Mode
Anonymous BlockUser 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.
There are the two types of controllers -
  1. Standard Controller
  2. 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.
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 -
  1. extensions
  2. Standard List Controller
  3. 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.
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.
  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 3different 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:
    1. 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.
    2. Static variables also won't occupy space in the view state.
    3. 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.
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')
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.
On VF page we should use -
?
1
2
<apex:pagemessages>
</apex:pagemessages>
In Apex Class, we should use -
?
1
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 -
  1. CONFIRM
  2. ERROR
  3. FATAL
  4. INFO
  5. WARNING
on a standardController page to display the multiple records we use recordSetVar (without the support of any extensions class).
dataTable: Display the records without standard salesforce look and feel.
pageBlockTable: Display the records with standard salesforce look and feel.
1000
For the page tag we can enable readOnly attribute value as true so that -
  • Number of query rows will increased from 50000 to 1 millionrows.
  • Number of records displayed on VF page will be increased from 1000 to 10000
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.
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.
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.
Wrapper Class is nothing but list of instances of a certain class. Follow the below steps to create the wrapper list -
  1. Create an Apex Class say 'MyWrapper'(It can be inside/outside of the controller class.)
  2. Declare the necessary variables in the class to store the information.
  3. Create a list called 'MyWrapList' for 'MyWrapper'.
  4. Create multiple instances for 'MyWrapper' and store it in 'MyWrapList' list.
  5. Access 'MyWrapList' list from VF page.
In the below scenarios, we have to go for 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.
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.
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
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.
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.
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.
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.
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.
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 -
    ?
    1
    <apex:outputlink value="/apex/samplePage">click<apex:param name="EN" value="Success"></apex:param></apex:outputlink>
  2. From pageReference: Upon calling an action method while returning pageReference we can append parameters. See the below example -
    ?
    1
    2
    3
    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 teh parameters.
    Click here for the reference.
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=someURLparameter to the URL.
Like VF page we can create visualforce component. Syntax -
?
1
2
3
<apex:component>
 Your code here.
</apex:component>
We can use only controller and extensions attribute for the VF component. standardController cannot be used.
When to use VF Component?
  1. If you want to reuse the VF page logic.
  2. If the VF page logic is huge and if you want to split into different pieces.
We can request VF page in two ways -
  1. 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
  2. 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

Ajax Functions