Friday 10 March 2017

Start learn to code- Apex , Visual force


Standard Controller Example

Write Visualforce page to create new Account

<apex:page standardController="Account">
<Apex:form >
<Apex:pageblock title="Create new account">
<Apex:pageBlockButtons >
<Apex:commandButton value="save" action="{!save}"/>
</Apex:pageBlockButtons>

<apex:pageBlockSection title="Enter Account details" columns="2" >

<apex:inputField value="{!account.name}"/>
<Apex:inputField value="{!account.type}"/>
<apex:inputField value="{!account.AccountNumber}"/>
</apex:pageBlockSection>
</Apex:pageblock>
</apex:form>
</apex:page>
                                        

Write visualforce page to create new contact using standard controller


<apex:page standardController="Contact">
<apex:form >
<apex:pageblock title="Enter contact Details">
<Apex:pageblocksection >
<apex:inputField value="{!contact.firstname}"/>
<apex:inputField value="{!contact.lastname}"/>
<apex:inputField value="{!contact.AssistantName}"/>
<apex:inputField value="{!contact.AssistantPhone}"/>
<apex:inputField value="{!contact.Department}"/>
<apex:inputField value="{!contact.email}"/>
<apex:inputField value="{!contact.Accountid}"/>
</Apex:pageblocksection>

<Apex:pageBlockButtons location="bottom">

<apex:commandButton value="Save" action="{!Save}"/>

</Apex:pageBlockButtons>
</apex:pageblock>
</apex:form>
</apex:page>
 

Display Account details of specific Account


Create visualforce page as follows and saved as Accountliststandard.


<apex:page standardController="Account">
 <apeX:pageBlock title="Account Details">
  <apex:pageBlockSection >
  
  <apex:outputField value="{!Account.name}"/>
  <Apex:outputField value="{!account.AccountNumber}"/>
  <Apex:outputField value="{!account.AnnualRevenue}"/>
  <Apex:outputField value="{!account.billingstreet}"/>
  <Apex:outputField value="{!account.billingcity}"/>
  <Apex:outputField value="{!account.billingcountry}"/>
  <Apex:outputField value="{!account.Industry}"/>
  
  </apex:pageBlockSection>
  </apeX:pageBlock>
</apex:page>

Click on preview button and pass account id.

https://c.ap2.visual.force.com/apex/Accountliststandard?id=0012800001AikIt

Get/Set Method salesforce

Get

The "get" method is used to pass data from your Apex code to your Visualforce page.. In our example we are not passing any value.. hence, when your page loads initially the textbox will have a empty value..

Apex Controller

Public class get_set_method
{

public string usrinput{get;set;}

public void getmethod()
{
usrinput='hello';
}
}

Visualforce page

<apex:page controller="get_set_method">
  <apex:form >

  <apex:pageblock >
  <apex:pageBlockSection >
  <apex:outputText value="Check Value"/>
  <apex:inputtext value="{!usrinput}"/>
  </apex:pageBlockSection>
  </apex:pageblock>

  </apex:form>

</apex:page>

When you save the page then you will get empty value.

output

Check Value        

We can pass value from apex code to visualforce page. In below example in constructor, we have set usrinput value  as 'I am fine!'. When visualforce page will load then it will show this value.

Apex Controller

Public class get_set_method
{

public string usrinput{get;set;}

public get_set_method()
{
usrinput='I am fine!';

}
public void getmethod()
{
usrinput='hello';
}
}


Set 

Set method is used to pass value from visualforce page to apex class.

Apex controller

Public class get_set_method
{

public string usrinput;

public string getusrinput(){
return 'hello';
}

public void setusrinput (string usrinput)
{
usrinput=this.usrinput;

}
}

Visualforce page

<apex:page controller="get_set_method">
  <apex:form>
    <apex:outputlabel value="Enter your name here"/>
       <apex:inputtext value="{!usrinput}">
           <apex:actionsupport event="onclick" rerender="display" />
       </apex:inputtext>                   
    <apex:outputpanel id="display">
        <apex:outputtext value="The name entered is {!usrinput}"/>
    </apex:outputpanel>   
    
            <apex:actionStatus id="counterStatus" 
                           startText=" (incrementing...)" 
                           stopText=" (done)"/>

                                           
  </apex:form>    

</apex:page>


Communicate Between Visualforce page and Apex


In this example, we will pass value from visualforce page to apex.

Public class get_set_method
{

public string usrinput{get;set;}
public string usroutput{get;set;}

public void Display()
{
usroutput=usrinput;
}
}

Visualforce page

<apex:page controller="get_set_method">
  <apex:form >
 
   <apex:pageBlock title="Input section">
<Apex:outputLabel value="input Value is:"/>
   <Apex:inputtext value="{!usrinput}" />

<apex:commandButton action="{!display}" value="Click me" reRender="panel"/>

</apex:pageBlock>

<apex:pageblock title="Output section">
<apex:outputPanel id="panel">
<Apex:outputLabel value="Output Value is:"/>
   <apex:outputText value="{!usroutput}"  />
   </apex:outputPanel>
</apex:pageblock>
                                         
  </apex:form>  
</apex:page>

Example to show sum of two userinput fields using visualforce page


<apex:page controller="sum">
<apex:form >
<Apex:pageblock title="Add Two Numbers">
<apex:pageBlockSection >
<Apex:outputLabel value="First Value"/>
<apex:inputtext value="{!a}"/>
<Apex:outputLabel value="second value"/>
<Apex:inputtext value="{!b}"/>
</apex:pageBlockSection>
<apex:pageBlockSection >
<Apex:outputLabel value="Sum is"/>
<aPEX:OUTputText value="{!c}"/>
<apex:commandbutton value="Show" action="{!display}"/>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>

Difference in ‘Rendered’, ‘Render As’, ‘ReRender’ 


RenderAs
By using renderAs we can generate visualforce page output in pdf.

<apex:page standardController="Account" showheader="false" standardStylesheets="false" extensions="AccountDisplay" renderAs="pdf">

Rendered
Visualforce page component can be hidden or shown on the basis of boolean variable value. It is used to show and hide elements works like css in general we can say display properties

<apex:pageBlock rendered=”{!ShowpageBlock}”>
Account Name  :<apex:outputField value=”{!Account.Name}”/>
Account Number :<apex:outputField value=”{!Account.AccountNumber}”/>
</apex:pageBlock>

Rerender is used when you want to refresh only a portion
reRender= “pgblck1, pgbcl2”

http://fkrt.it/1mOAn!NNNN

5 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Thanks a lot. I will be sharing new post soon.

    ReplyDelete
  3. i'm looking for salesforce job...but i don't have the knowledge about development. i tried the force.com platform and it looks easier to develop so, would you like to suggest something that boosts my career in salesforce.
    thank you!!

    ReplyDelete
    Replies
    1. start with saleaforce apex and visual force workbooks. Write code in your developer sandbox. You will find lots of videos in YouTube regading apex and visual force

      Delete

Please add your comments here