Ads

Friday, 3 May 2013

View state in Salesforce?

 The view state of a web page is composed of all the data that's necessary to maintain the state of the controller during server requests (like sending or receiving data). Since the view state contributes to the overall size of your page, performance of a page can depend on efficiently managing the view state. The View State tab in the development mode footer provides information about the view state of your Visualforce page as it interacts with Salesforce.
Note:
  • Minimize number of form on a page.  Use apex:actionRegion instead of using 2 or more forms.
  • Refine your SOQL to only retrieve the data needed by the page.
  • All public and private data members present in Standard, Custom and Controller extensions are saved.
  • The transient variables are not passed to view state and therefore not stored in View State.

Difference between rendered, renderAs and reRender in Visualforce page?

render - Boolean value (if it is true, it displays the block else it will be hidden)

reRender - ID. Particular block will be refreshed.

renderAs - Display the page as pdf, excel, etc...

Calling Controller method using Javascript in Visualforce page?

Visualforce page:

<apex:page controller="sample">
    <!-- Calling Controller Method from Visualforce page -->
   
    <!-- Javascript -->
    <script type="text/javascript">
        function jsfind()
        {
            callfind();
        }
    </script>
   
    <apex:pagemessages />
   
    <apex:form >
   
    <apex:actionFunction name="callfind" action="{!searchAndfind}" reRender="a" />
   
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:outputLabel >Query:</apex:outputLabel>
            <apex:inputText value="{!qry}"/>       
        </apex:pageblockSection>
       
        <apex:pageBlockButtons >
            <apex:commandButton value="Query" onclick="jsfind()"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
   
    </apex:form>

</apex:page>


Apex Class: 

public class sample
{

    public String qry{get;set;}

    public pageReference searchAndfind()
    {
        pageReference pg = new pageReference('http://www.google.com/search?q='+qry);
        pg.setRedirect(true);
        return pg;
    }      
}

How to open another Visualforce page from current Visualforce page in Salesforce?

Visualforce page:

<apex:page controller="sample">
            <apex:commandButton value="Open" action="{!openPage}"/>   
</apex:page>


Apex Class: 

public class sample
{
    public pageReference openPage()
    {
        pageReference pg = new pageReference('/apex/Name_Of_The_Page');
        pg.setRedirect(true);
        return pg;
    }      
}

Hide and show multiple pageBlocks in Salesforce?

Visualforce page:

<apex:page controller="sample">
   
    <apex:form >
   
    <apex:pageBlock >
        <apex:commandButton value="A Section" action="{!callA}"/>
        <apex:commandButton value="B Section" action="{!callB}"/>
    </apex:pageBlock>
   
    <apex:pageBlock rendered="{!Abool}">
       <apex:outputText value="This is A section"/>
    </apex:pageBlock>   
   
    <apex:pageBlock rendered="{!Bbool}">
       <apex:outputText value="This is B section"/>
    </apex:pageBlock>     

    </apex:form>

</apex:page>


Apex Class:

public class sample
{
    public Boolean Abool {get;set;}
    public Boolean Bbool {get;set;}
   
    public sample()
    {
        Abool = false;
        Bbool = false;   
    }  
    public void callA()
    {
        Abool = true;
        Bbool = false;
    }  
    public void callB()
    {
        Abool = false;
        Bbool = true;   
    }    
}

Email validation using Apex in Salesforce?

Visualforce page:

<apex:page controller="testController">
    <apex:form id="myform">
    <apex:pagemessages />
    <apex:pageBlock id="myblock">
        Email Address: <apex:inputText value="{!email}" id="email"/><br/><br/>
        <apex:commandButton value="Click me!" action="{!checkEmail}"/>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Code:

public class testController
{
    public String email { get; set; }
    public void checkEmail()
    {

        if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email))
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email')); 

        }
    }
}

How can we check whether an object has accessibility on visualforce page?


To check whether an object has accessibility on visualforce page, use the below code

{!$ObjectType.MyCustomObject__c.accessible}
It returns true or false.
Sample code:
<apex:page >
    Accessibility for Object 'Member__c' is {!$ObjectType.Member__c.accessible}
</apex:page>  

Output: