Thursday, 7 November 2013

PAGEBLOCKTABLE EDIT/DELETE FUNCTIONALITY

A basic example to edit/delete records individually.
Here i have taken two pages editdelete and editfun
on clicking edit in page 1 i am redirected to page 2 to edit the record and save it, after saving i am redirected to sobject detail page.
Below is code for page 1 :-

 <apex:page showHeader="false" sidebar="false" controller="editcon" tabStyle="Acc__c">  
 <apex:form >  
 <apex:sectionHeader title="edit/delete"/>  
 <apex:pageBlock >  
 <apex:pageBlockTable value="{!stun}" var="stu">  
 <apex:column >  
 <apex:param name="sid" value="{!stu.id}"/>  
 </apex:column>  
 <apex:column value="{!stu.name}"/>  
 <apex:column value="{!stu.Name__c}"/>  
 <apex:column value="{!stu.Age__c}"/>  
 <apex:column value="{!stu.Gender__c}"/>  
 <apex:column headerValue="Action">  
 <apex:commandLink value="EDIT" action="{!edit}">  
 <apex:param name="cid" value="{!stu.id}" assignTo="{!ecid}"/>  
 </apex:commandLink>  
 &nbsp;&nbsp;&nbsp;&nbsp;  
 <apex:commandLink value="DELETE" action="{!deletecon}">  
 <apex:param name="cid" value="{!stu.id}" assignTo="{!dcid}"/>  
 </apex:commandLink>  
 </apex:column>  
 </apex:pageBlockTable>  
 </apex:pageBlock>  
 </apex:form>   
 </apex:page>  
page 1 screen shot:-

controller class:-
 public class editcon {  
 list<Acc__c> stun = new list<Acc__c>();  
 list<Acc__c> stun1 = new list<Acc__c>();   
 public string ecid{get;set;}  
 public string dcid{get;set;}  
   public PageReference deletecon() {  
   stun1 =[Select Name, name__c, age__c from Acc__c where id=:dcid];  
     delete stun1;  
     pagereference pg= new pagereference('/apex/editdelete');  
     pg.setRedirect(True);      
     return pg;  
   }  
  public PageReference edit() {  
   pageReference pg = new pagereference('/apex/editfun?id='+ecid);  
   pg.setRedirect(false);  
     return pg;  
   }  
  public list<Acc__c> getStun() {  
   stun = [select name, name__c, age__c,gender__c from Acc__c];  
     return stun;  
   }  
 }  
}


page  2:-
 <apex:page showHeader="false" sidebar="false" standardController="Acc__c"tabstyle="Acc__c">  
 <apex:form >  
 <apex:sectionHeader title="Edit the record"/>  
 <apex:pageBlock tabStyle="Acc__c">  
 <apex:pageBlockButtons location="both">  
 <apex:commandButton value="save" action="{!save}"/>  
 </apex:pageBlockButtons>  
 <apex:pageBlockSection >  
 <apex:inputField value="{!Acc__c.Name__c}"/>  
 <apex:inputField value="{!Acc__c.Age__c}"/>  
 </apex:pageBlockSection>  
 </apex:pageBlock>  
 </apex:form>  
 </apex:page>  
screen shot of page 2:-

Monday, 4 November 2013

Example to Display Page block Table on button click

<apex:page controller="showpgbtable" >
 <apex:form >
  <apex:commandButton value="showtable" action="{!accountlist}" reRender="panel"/>
   <apex:outputPanel id="panel" >
    <apex:pageBlock rendered="{!rendered}" >
     <apex:pageBlockTable value="{!accounts}" var="a" >
      <apex:column value="{!a.name}"/>
     </apex:pageBlockTable>
    </apex:pageBlock>
   </apex:outputPanel>
 </apex:form>
</apex:page>

//controller class
public class showpgbtable {
 public list<Account> acc1 = new list<account>();
public Boolean rendered{set;get;}
public showpgbtable(){ //constructor
rendered =false;
}
 public pageReference Accountlist(){
    acc1 = [select id,name from account ];
     rendered  = true;
    return null;
   }
   public list<account> getaccounts(){
   return acc1;
   }
Screen Shots:-

}

Sunday, 3 November 2013

UPLOADING IMAGE IN SALESFORCE THROUGH VF PAGE

A very common requirement we come across is uploading image/photo through vf pages. While doing this task we need to keep in mind that we can't directly display image in visualforce page in salesforce. It should be stored at some place- There are 3 places where we can store our images

  1. Documents
  2. Static Resources
  3. Attachments.
Below i am illustrating an example of uploading and displaying image in visualforce using Documents sobject.


<!--vf page code-->
 <apex:pageBlockSection title="Upload Photo" collapsible="false" columns="1" >  
 <apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!doc}" height="100"       width="100" rendered="{!showimage}" /> /*contains the url of documents,{!doc} gives the image dynamically */  
  <apex:inputFile value="{!Document.body}" filename="{!document.name}" />  
 <center><apex:commandButton value="Upload" action="{!save}" /></center>  
  </apex:pageBlockSection>  
//apex class code
 public boolean showimage{set;get;}  
  public final document t;  
   private ApexPages.StandardController stdcontroller;  
   public getdocumentid(ApexPages.StandardController stdcontroller) {  
   t = (Document) stdcontroller.getRecord();  
     t.folderid = UserInfo.getUserId(); //this saves record in My Personal Documents  
     this.stdcontroller=stdcontroller;  
     }    
  public pagereference save(){  
   this.stdcontroller.save();// this method implements the standard save method.  
   pagereference pg=new pagereference('/apex/vfpageimageupload');  
   showimage=true;  
   return pg;  
   }   
   List<document> d;  
   public id doc{set;}  
   public id getdoc()  
   {  
   d=[select id from document order by id desc limit 1]; //gets id of document inserted last in sobject  
   return d[0].id;// returns id value of list d  
   }   

Monday, 28 October 2013

selected record popup using radiobutton in salesforce

Radio buttons, We have a great usage of this to select or deselect records. Recently did this task and found it would be useful for some newbies.. I think the code is self explanatory

<!-- My Page-->

 <apex:page Controller="AccountTaskClass7" sidebar="false">  
  <style type="text/css">  
  .popup  
  {  
  background-color: pink;  
  border-width: 2px;  
  border-style: solid;  
  z-index: 9999;  
  left: 50%;  
  padding:10px;  
  position: absolute;  
  width: 400px;  
  margin-left: -250px;  
  top:80px;  
  }  
  .popupBg  
  {  
  background-color:yellow;  
  opacity: 0.20;  
  filter: alpha(opacity = 70);  
  position: absolute;  
  width: 100%;  
  height: 100%;  
  top: 0;  
  left: 0;  
  z-index: 9998;  
  }  
  </style>  
  <apex:form id="form">  
  <apex:pageblock id="pb">   
  <apex:pageBlockTable id="pbt" value="{!getAcountt}" var="act">  
  <apex:column headerValue="Account Name">  
  <apex:outputText value="{!act.name}"></apex:outputText>  
  </apex:column>  
  <apex:column headerValue="Detail">  
  <apex:outputPanel >  
  <apex:actionSupport event="onclick" action="{!detailPage}" rerender="popup,pb">  
  <input type="radio" name="det" />  
  <apex:param value="{!act.id}" assignTo="{!id}" name="id"/>  
  </apex:actionSupport>  
  </apex:outputPanel>  
  </apex:column>  
  </apex:pageBlockTable>  
 </apex:pageblock>  
 <!--popup code starts-->  
  <apex:outputPanel id="popup">  
  <apex:outputPanel styleClass="popupBg" layout="block" rendered="{!displayPopUp}"/>  
  <apex:outputPanel styleClass="popup" layout="block" rendered="{!displayPopUp}">  
  <apex:messages />  
  <center><h2>{!act_name} Account Information</h2></center>  
  <apex:panelGrid columns="2" >  
  <apex:outputLabel value="Account Name"/><apex:outputText value="{!act_name}" />  
  <apex:outputLabel value="Number of Employee"/><apex:outputText value="{!act_noe}" />  
  <apex:outputLabel value="Industry"/><apex:outputText value="{!act_ind}" />  
  <apex:outputLabel value="Annual Revenue"/><apex:outputText value="{!act_annual_rev}" />  
  <apex:commandButton value="cancel" action="{!closePopup}" rerender="popup,pb"/>  
  </apex:panelGrid>  
  </apex:outputPanel>  
  </apex:outputPanel>  
 <!-- end of popup code-->  
  </apex:form>  
 </apex:page>  

//controller class
 public with sharing class AccountTaskClass7 {  
  public decimal act_annual_rev { get; set; }  
  public String act_ind { get; set; }  
  public String act_name { get; set; }  
  public integer act_noe { get; set; }  
  public boolean displayPopUp { get; set; }  
  public String acct { get; set; }  
  public PageReference showPopup() {//popupmethod  
  displayPopup = true;  
  return null;  
  }  
 public PageReference closePopup() {//hide method  
  displayPopup = false;  
  return null;  
  }  
 public PageReference detailPage() {// action method which shows popup on selection  
  displayPopup = true;  
  system.debug('Select Item Id is ::: '+ApexPages.currentPage().getParameters().get('id'));  
  String id = ApexPages.currentPage().getParameters().get('id');  
  account ac = [select id,name,NumberOfEmployees,Industry,AnnualRevenue from account where id  
 =: id];  
  act_name = ac.name;  
  act_noe = ac.NumberOfEmployees;  
  act_ind = ac.Industry;  
  act_annual_rev = ac.AnnualRevenue;  
  return null;  
  }  
  public list<account> getGetAcountt() {  
  list<account> act_name = [select id,name from account];  
  return act_name;  
  }  
 }  
}


Screen shots:-

Monday, 13 May 2013

Simple Search


VF Code:
 <apex:page sidebar="false" showHeader="false" controller="ser" tabstyle="account">  
 <apex:form >  
 <apex:pageBlock >  
 <apex:panelGrid title="search your query">  
 <apex:outputText >Book name</apex:outputText>  
 <apex:inputText value="{!accname}"/>  
 <apex:commandButton value="Search" action="{!search}"/>  
 <apex:pageBlockTable value="{!accname1}" var="b">  
 <apex:column value="{!b.id}"/>  
 <apex:column value="{!b.name}"/>   
 </apex:pageBlockTable>  
 </apex:panelGrid>  
 </apex:pageBlock>  
 </apex:form>  
 </apex:page>  

Apex Class Code:
 public class ser {  
 public string accname{set;get;}  
 public string item{set;get;}  
 public string accname1{set;}  
 account bk = new account();  
 public pagereference search(){  
 bk = [select id,name from account where name = : accname];  
 return null;  
 }  
 public account getaccname1(){  
 return bk;  
 }  
 }