Saturday, May 18, 2019

Salesforce Interview Questions on Visualforce

  • What is action function in visualforce? 


With action function, you can directly invoke the controller method from javascript function using ajax request.


Action function must be called from javascript function or from a client-side event such as onclick.

Syntax:

<apex:actionFunction name="somename" action="{!callControllerMethod}" reRender="someid"/>



Let's take an exxample for creating contact,

Visualforce page:

<apex:page controller="createcontact">
<script>

  function javascriptmethod()
  {

  createcon();
  alert('contact created');

  }
  </script>

  <apex:form id="test">

  <apex:inputText value="{!contactname}"/>
  <apex:commandButton value="Create contact" onclick="javascriptmethod()"/>


  <apex:actionFunction name="createcon" action="{!callControllerMethod}" reRender="test"/>

  </apex:form>
</apex:page>


Controller:

public class createcontact{
public string contactname{get;set;}

public void callControllerMethod()
{

contact obj=new contact();
obj.lastname=contactname;
insert obj;

}

}

  • What is action support in visualforce? 

Basically, we use action support to support another visualforce component,
the events include "onchange", "onclick" etc.

Let's take an example,

Visualforce:

<apex:page controller="createcontact1">


  <apex:form id="test">

  <apex:inputText value="{!contactname}">
  <apex:actionSupport event="onchange"  action="{!callControllerMethod}" reRender="pageblockid"/>
  </apex:inputText>
  <apex:pageblock id="pageblockid">
 <apex:pageBlockTable value="{!contactList}" var="con">
 <apex:column value="{!con.name}"/>


 </apex:pageBlockTable>

  </apex:pageblock>

  </apex:form>
</apex:page>


Controller:

public class createcontact1{
public string contactname{get;set;}
public list<contact> contactList{get;set;}
public void callControllerMethod()
{

contactList=[select name from contact limit 1];

}

}

  • What is view state? 


Suppose we are having a requirement to register a user. If we are having multiple pages
which user need to fill, so under this case we need to maintain the state when the user is moving
from one page to other. For this, we use form tag. Governing limit for the form tag is 135 kb.
To avoid reaching the limit we should declare a variable as static or we need to mention transient
keyword before the variable as this does not occupy the state.

Solutions to reduce view state:


1)Use remote method as it is stateless.
2)use a transient keyword before variable which is not required to be retained as going forward.
3)Avoid form tag if not required as it will not create view state.

By reducing the view state the page load quicker so the performance of the page increases.

  • Explain controller in salesforce?

Standard controller in salesforce (Click on the link for complete details)

Custom controller in salesforce (Click on the link for complete details)

No comments:

Post a Comment