Ads

Showing posts with label VisualForce Pages. Show all posts
Showing posts with label VisualForce Pages. Show all posts

Thursday 16 May 2013

How to find object type from Salesforce record id?

Visualforce page:

<apex:page Controller="sample" sidebar="false" >
<apex:form >
    <apex:pageblock id="pg" >
        <apex:pageblockSection >
            <apex:pageBlockSectionItem >Record Id</apex:pageBlockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext value="{!recId}" /></apex:pageblockSectionItem>
        </apex:pageblockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Find" action="{!find}" reRender="pg"/>
        </apex:pageBlockButtons>
        <apex:outputText >The object type is : {!objType}</apex:outputText>
    </apex:pageblock>
</apex:form>  
</apex:page>


Apex: Controller:

public with sharing class sample
{
    public Id recId {get;set;}
    public String output {get;set;}
    public Schema.SObjectType objType {get;set;}
   
    public void find()
    {
        objType = recId.getSobjectType();
        System.debug('Object Type is ' + objType);
    }
}


Output:


Sunday 5 May 2013

To send parameters from one visualforce page to another?

The below code is used to send parameters from one visualforce page to another visualforce page. 

Visualforce:

<apex:outputlink value = "/apex/sample">
  <apex:param name = "msg" value = "hi"/>
</apex:outputlink>

Apex:

public String message = System.CurrentPagereference().getParameters().get('msg');

Friday 3 May 2013

To get values from input field in controller extension using Apex?

The following code explains how to get values from <apex:inputfield> in controller extension using apex 

Visualforce:

  <apex:pageblock title="Blog" mode="edit" >
    <apex:pageBlockButtons >
      <apex:commandButton action="{!nxt}" value="Next"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection columns="1">
      <apex:inputField value="{!Blog__c.name}"/>
      <apex:inputField value="{!Blog__c.URL__c}"/>
    </apex:pageBlockSection>
  </apex:pageblock>

Apex:

  Public Blog__c blg; 
  this.blg = (Blog__c)controller.getRecord();

  String nam = blg.name;
  Sting url = blg.URL__c;

Component in Salesforce?

In Salesforce component is a reusable piece of code developed using Visual force and Apex controller.

Example:

<!-- Page -->

<apex:page>

    <c:myComponent myValue="My component's value" borderColor="red" />

</apex:page>


<!-- Component:myComponent -->     
<apex:component>

    <apex:attribute name="myValue" description="This is the value for the component."  type="String"required="true"/>

    <apex:attribute name="borderColor" description="This is color for the border."  type="String" required="true"/>

    <h1 style="border:{!borderColor}">

        <apex:outputText value="{!myValue}"/>

    </h1>

</apex:component>

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:

How to Access Wrapper class variable in Visualforce page?

Apex Code:

public class a
{

     //accessing wrapper class variable
     public w var {get;set}
    //wrapper class
    public class w
    {
        public String str;
    }
}


Visualforce page:

<apex:outputText value="{!var.str}"/>

Retrieving parent record from child record in Salesforce?


Visualforce Page:

<apex:page controller="sample" >
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!intList}" var="i">
            <apex:column value="{!i.Name}"/>
            <apex:column value="{!i.Member__r.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Controller:

public class sample
{   
    public List<Interest__c> intList {get;set;}
    public sample()
    {
        String sql = 'SELECT Name, Member__r.Name FROM Interest__c';
        intList = Database.Query(sql);
    }   
}


Output:

@RemoteAction in Visual force page?


JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript remoting has three parts:
  • The remote method invocation you add to the Visualforce page, written in JavaScript.
  • The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
  • The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
 To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
  • namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
  • controller is the name of your Apex controller.
  • method is the name of the Apex method you’re calling.
  • parameters is the comma-separated list of parameters that your method takes.
  • callbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
  • configuration configures the handling of the remote call and response. Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
Visualforce Page:

<apex:page controller="sample">
    <script type="text/javascript">
    function getAccountJS()
    {
        var accountNameJS = document.getElementById('accName').value;       
        sample.getAccount( accountNameJS,
        function(result, event)
        {
            if (event.status)
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id;
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name;
            }
            else if (event.type === 'exception')
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } else
            {
                document.getElementById("errors-js").innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id="accName" type="text" />
    <button onclick="getAccountJS()">Get Account</button>
    <div id="errors-js"> </div>
    <apex:pageBlock id="theBlock">
        <apex:pageBlockSection id="thePageBlockSection" columns="2">
            <apex:pageBlockSectionItem id="theFirstItem">
                <apex:outputText id="accId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="theSecondItem" >
                <apex:outputText id="accNam" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>


Apex Controller:

global class sample
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public sample() { }
   
    @RemoteAction
    global static Account getAccount(String accountName)
    {
        account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
        return account;
    }
}


Ouptut:

How to Include One Visualforce Page within Another?

<apex:page sidebar="false" showHeader="false">
    <p>Test Before</p>
    <apex:include pageName="MainPage"/>
    <p>Test After</p>
</apex:page>