Thursday, July 31, 2014

Visualforce Action Region Example.

Visualforce Action Region <apex:actionRegion> Example.


Hi,
In this post i am giving an example of <apex:actionRegion> usage in visualforce. it will use to achieve AJAX functionality to partial page refresh.

Controller Class:


public class ActionRegionCLS{
 public String reqAccNumber{get;set;}
 public Account accObj{get;set;}

 public ActionRegionCLS(){
  accObj = new Account();
 }
 public void fillRecord(){
  if(reqAccNumber !=null && reqAccNumber !=''){
    List<Account> accLst = new List<Account>();
    accLst = [select id,Name,AccountNumber,Type,AnnualRevenue from Account where AccountNumber =:reqAccNumber];
    if(accLst !=null && accLst.size()>0){
     accObj = accLst[0];
    }
    else{
   
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,' No Records found with this Account Number:'+reqAccNumber);
     ApexPages.addMessage(myMsg);
     accObj  = null;
  
    }
  }
 }

 }

Visualforce Page :

<apex:page controller="ActionRegionCLS" >
 <apex:pagemessages id="msgs"></apex:pagemessages>
 <apex:form >
  <apex:pageblock id="pb">
   <table>
     <apex:actionRegion >
      <tr>
        <td> <apex:outputLabel value="Enter Account Number"> </apex:outputLabel> </td>
        <td>
        
         
          <apex:inputtext value="{!reqAccNumber}"/>
          <apex:commandButton value="Retrive" action="{!fillRecord}" rerender="pb,msgs"/>
          </td>
       </tr>  
     </apex:actionRegion>
     <tr>
      <td> <apex:outputLabel value="Account Name"> </apex:outputLabel> </td>
      <td>
        <apex:inputField value="{!accObj.Name}"/>
      </td>
     </tr>
     <tr>
     <td> <apex:outputLabel value="Account Type"> </apex:outputLabel> </td>
      <td>  <apex:inputField value="{!accObj.Type}"/> </td>
     </tr>
   </table>
  </apex:pageblock>
 </apex:form>
</apex:page>


Output :

save image


once you enter account number value in Account Number text box and click on Retrive button it going to get the details of "Account Name", "Account Type" with out reloading entire page.using action region we are updating particular HTML area section with Rerender Tag.


          <apex:commandButton value="Retrive" action="{!fillRecord}" rerender="pb,msgs"/>

  

save image



 

Wednesday, July 30, 2014

Visualforce Action Function Example.

Visualforce Action Function Example.


Hi, 
In this post i am giving an example of simple usage of <apex:actionFunction> tag in visualforce page to call a apex method from Java script.

Controller Class:


public class ActionFunctionCLS {
  public Account actObj{get;set;}
   PageReference prf= null;
    public ActionFunctionCLS(){
      actObj = new Account();
    }
 
   
    public pagereference Saverec(){
   if(actobj.Name !=''){
    insert actobj;
   
   }
   else{
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
     ApexPages.addMessage(myMsg);
   
   }
   if(actobj.id !=null){
  
      // Send the user to the detail page for the new account.
      prf = new PageReference('/'+actobj.id);
      prf.setRedirect(true);
   
   }
   return prf;
    }

}


Visualforce Page:

<apex:page controller="ActionFunctionCLS" id="pg" >
  <script>
   function recSave(){
    var accountType = document.getElementById('pg:fm:pb:pbs:actType').value;
    alert('accountType -->'+accountType);
    if(accountType != 'Prospect'){
     alert('You Should Select Prospect to Save the Record');
     return false;
    }
    else{
     saveAccount(); //this is the function name which calls our action function from java Script.
     return true;
    }
   }
 
  </script> 

 <apex:form id="fm">
  <apex:actionfunction name="saveAccount" action="{!Saverec}" />
   <apex:pageBlock id="pb">
     <apex:pagemessages ></apex:pagemessages>
     <apex:pageblockButtons >
      <apex:commandButton value="Save" onclick="recSave();return false;"/>    
     </apex:pageblockButtons>
    
     <apex:pageblockSection id="pbs">
       <apex:inputField value="{!actobj.Name}" id="actName"/>
       <apex:inputField value="{!actobj.type}" id="actType"/>
     </apex:pageblockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page>





              




Once you select Account Type as "Prospect" Javascript method will call the <apex:actionfunction name="saveAccount" action="{!Saverec}" /> and controller method is going to execute.



That's it..


Docusign for Salesforce Demo.

Docusign for Salesforce Demo.


Hi,
Here is the Docusign for Salesforce Demo video.







Monday, July 28, 2014

How to use apex:selectOptions and prepare picklist values in Visualforce Page.

How to use apex:selectOptions and prepare picklist values in Visualforce Page.

Hi,
In this post i am using  <apex:selectOptions >,<apex:selectRadio>,<apex:selectList> tags. Here from controller i am preparing pick-list values and set in visualforce page.

Controller: 

public class SelectOptionController {
 public List<SelectOption> countrieLst {get;set;}
 public List<SelectOption> hobbiesLst {get;set;}
 public List<SelectOption> genderLst {get;set;}

 public String selectedCountry{get;set;}
 public String selectedHobby {get;set;}
 public String selectedGender {get;set;}
 public String selectedValues {get;set;}


 public SelectOptionController(){
  countrieLst = new List<SelectOption>();
  hobbiesLst = new List<SelectOption>();
  genderLst = new List<SelectOption>();
  
  countrieLst.add(new SelectOption('','--None--'));
  countrieLst.add(new SelectOption('India','India'));
  countrieLst.add(new SelectOption('China','China'));
  countrieLst.add(new SelectOption('US','US'));
  
  /* preparing picklist values in VF page.
  hobbiesLst.add(new SelectOption('','--None--'));
  hobbiesLst.add(new SelectOption('Swimming','Swimming'));
  hobbiesLst.add(new SelectOption('Reading','Reading'));
  hobbiesLst.add(new SelectOption('Cricket','Cricket'));
  */

  genderLst.add(new SelectOption('Male','Male'));
  genderLst.add(new SelectOption('Female','Female'));
   
 }

 public pageReference show(){
  selectedValues = 'Selected country:'+selectedCountry +'-->Selected Hobbies:'+selectedHobby+' Gender: -->'+selectedGender;
  return null;

 }


}


Visualforce page:

<apex:page controller="SelectOptionController">
 <apex:form > 
   <apex:commandbutton value="Show Values" action="{!show}"/>
   
   <apex:pageBlock >    
    <apex:outputLabel > Countries : </apex:outputLabel>
    <apex:selectList size="1" value="{!selectedCountry}">
      <apex:selectOptions value="{!countrieLst }"/>
    </apex:selectList> <br/>
    
    <apex:outputLabel > Gender : </apex:outputLabel>
    <apex:selectRadio value="{!selectedGender}"> 
     <apex:selectOptions value="{!genderLst}"/>
    </apex:selectRadio>  
    
     <apex:outputLabel > Hobbies :</apex:outputLabel>
    <apex:selectList size="1" value="{!selectedHobby}">
     <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
     <apex:selectOption itemLabel="Swimming" itemvalue="Swimming"></apex:selectOption>
     <apex:selectOption itemLabel="Reading" itemvalue="Reading"></apex:selectOption>
     <apex:selectOption itemLabel="Cricket" itemvalue="Cricket"></apex:selectOption>
    </apex:selectList>     
    </apex:pageBlock> 
   
   <apex:pageBlock title="selected values">
    <apex:outputText value="{!selectedValues}" style="color:red"/>   
   </apex:pageBlock>
 </apex:form>

</apex:page>


Test Class:

@isTest
Public class SelectOptionController_Test {
 static testmethod void testSelectOptionController(){
  //Test converage for the myPage visualforce page
  PageReference pageref = Page.SelectOptionVFExample;
  Test.setCurrentPageReference(pageref);
  
  // create an instance of the controller
  SelectOptionController sop= new SelectOptionController ();
  sop.selectedCountry ='India';
  sop.selectedHobby='Swimming';
  sop.selectedGender ='Male';
  sop.show();
  
  system.assertEquals('India',sop.selectedCountry);
  system.assertEquals('Swimming',sop.selectedHobby);
  system.assertEquals('Male',sop.selectedGender);
  }
}



out put: 

save image




 
| ,