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  
   }