Sunday 15 September 2013

Hide Recycle Bin from Home Page Layout 

 Salesforce doesn't provide specifications to remove Recycle Bin from Home Page.But there are situations which demands that the Recycle Bin shouldn't be shown over the Home Page. In such adverse situations JavaScript is our Best Friend.


First Let me tell you about this Dustbin of Salesforce.

The Recycle Bin link in the sidebar lets you view and restore recently deleted records for 15 days before they are permanently deleted.

Follow the following steps to Hide the Recycle Bin.
 

Click on Name > Setup > Customize > Home > Home Page Components.
Click on New to add a New Custom Component. 

Make sure that you have checked WIDE COMPONENT and SHOW HTML Checkbox. 

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<span id="hideMyParentsp">
</span>
<script>
$(document).ready(function() {  $('#sidebarDiv #hideMyParentsp').parent().parent().hide();      });
</script>

<script type="text/javascript">
window.onload = function(){
var x=document.getElementsByTagName("img");
for(var i=0;i<x.length;i++) {
  if(x[i].title=='Recycle Bin')  {
     var dtag = x[i].parentNode.parentNode.parentNode; dtag.style.visibility='hidden';
      }
 }
var xx=document.getElementsByTagName("span");
for(var ii=0;ii<xx.length;ii++) {
  if(xx[ii].innerHTML=='Recycle Bin')  {
 xx[ii].style.display='none';
  }
 }
 };
 </script>
 
 
 

 Click on Save.

Next we need to add this component on Home Page Layout.For this Follow the following Steps
   
    Click on Name > Setup > Customize > Home > Home Page Layout.

 

Select "Edit" for your Layout from which you want to remove Recycle Bin.
    Check the Component which you just created in Wide Column Display , Click Next

 

Click Save , Additionally you can see the preview.


  You can see the changes in your Home Page. Recycle Bin is No More. You can say that his soul is with us [as it is just hidden] but his body left the mortal world. 

 

 

 

  

Sunday 11 August 2013

Pagination using StandardSetController

I have struggled with pagination issue and finally implemented with StandardSetController.

Controller:


public with sharing class Pagination_min {

    public PageReference print() {
        pageReference pg = new PageReference('/apex/printPage');
        return pg;
    }

    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public  list<JobSuite__job__c> lstJob = new list<JobSuite__Job__c>();
    public set<Id> cmpId = new set<Id>();
    public Pagination_min()
    {
       
        lstJob = [select Name,JobSuite__JS_Client__c,JobSuite__Campaign__c from JobSuite__Job__c  ];
       
        for(JobSuite__Job__c objob: lstJob)
        {
            cmpId.add(objob.JobSuite__Campaign__c);
        }
   
    }
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 1;
                string status = 'Active';
               
                //string queryString = 'Select Name from JobSuite__JOb__c where JobSuite__Status__c =:Status';
                system.debug('Ids...'+cmpId);
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, name FROM JobSuite__Campaign__c where  Id IN:cmpId order By Name]));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
   
    Public List<JobSuite__Campaign__c> getAccounts(){
        List<JobSuite__Campaign__c> accList = new List<JobSuite__Campaign__c>();
        for(JobSuite__Campaign__c a : (List<JobSuite__Campaign__c>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
   
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
}



Page:




<apex:page controller="Pagination_min">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:repeat value="{!Accounts}" var="a">
                 {!a.Name}
               
                 <apex:commandbutton action="{!print}" value="Print"/>                
            </apex:repeat>
            <apex:panelGrid columns="7">
               
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
               
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}  of {!noOfRecords}</apex:outputText>
               
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Sunday 25 November 2012

Salesforce Pagination Using OFFSET keyword in SOQL

As per salesforce Governor limitations  we can't get more than 1000 records in visualforce page. To display large number of records we can use pagination using OFFSET keyword in controller. OFFSET is use full feature for pagination.


Apex Controller:

public class PaginationController{
    // initial query string for contact
    String queryString = 'Select Id,FirstName,LastName,Email,Account.Name from Contact';
    
    Integer queryLimit;
    Integer offset;
    
    // for active and deactive page naviagator
    public Boolean firstOff{set;get;}    // previous part
    public Boolean lastOff{set;get;}     // next part
    
    public String limits{set;get;}
    public Integer pageNumber{set;get;}
    
    Integer listSize;    // for total contact size
    
    // constructor
    public PaginationController(){
        firstOff = false;
        queryLimit = 10;
        offset = 0;
        limits = '10';
        pageNumber = 1;
        
        aggregateResult res = [select COUNT(id) cnt from Contact ];
        
        // fill size of all contact
        listSize = Integer.valueOf(res.get('cnt'));
        
        // initialy check page more then 1 or not
        if(listSize > queryLimit) lastOff = true;
        else lastOff = false;
    }
    
    // get record according to limit and offset
    public List<Contact> getRecords(){
       return (List<Contact>)database.query(queryString+' LIMIT '+queryLimit+' OFFSET '+offset);
    }
    
    // navigate on next page
    public void next(){
        offset += queryLimit;
        if(offset+queryLimit >= listSize) lastOff = false;
        firstOff = true;
        pageNumber++;
    }
    
    // navigate on previous page
    public void previous(){
        if(offset-queryLimit <= 0){
            offset = 0;
            firstOff = false;
        }
        else offset -= queryLimit;
        lastOff = true;
        pageNumber--;
    }
    
    // switch on first page
    public void first(){
        offset = 0;
        firstOff = false;
        lastOff = true;
        
        pageNumber = 1;
    }
    
    // switch on last page
    public void last(){
        // set page number of and offset
        if(Math.Mod(listSize,queryLimit) == 0){
            offset = listSize-queryLimit;
            pageNumber = listSize/queryLimit;
        }
        else{
            offset = (listSize/queryLimit)*queryLimit;
            pageNumber = (listSize/queryLimit)+1;
        }
        
        lastOff = false;
        firstOff = true;
    }
    
    // for record limits
    public List<SelectOption> getItems(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('10','10'));
        options.add(new SelectOption('20','20'));
        options.add(new SelectOption('50','50'));
        options.add(new SelectOption('100','100'));
        return options;
    }
    
    // change query limit
    public void changeLimit(){
        // set query limit
        queryLimit = Integer.valueOf(limits);
        
        offset = 0;
        firstOff = false;
        
        // initialy check page more then 1 or not
        if(listSize > queryLimit) lastOff = true;
        else lastOff = false;
        
        // set page number
        pageNumber = 1;
    }
    
    // for show current record numbers
    public String getRecordInfo(){
        integer lastLimit;
        if(offset+queryLimit > listSize) lastLimit = listSize;
        else lastLimit = offset+queryLimit;
        return (offset+1) + ' - ' + lastLimit + ' of '+listSize;
    }
    
    // return total page number
    public Integer getTotalPage(){
        if(Math.Mod(listSize,queryLimit) == 0) return listSize/queryLimit;
        else return (listSize/queryLimit)+1;
    }
    
    // for direct page switching
    public void pageNavigation(){
    
        /* if user enter more then number ot total page number than
           set the value last page number in PageNumber. */
        if(Math.Mod(listSize,queryLimit) == 0 && pageNumber > listSize/queryLimit)
            pageNumber = listSize/queryLimit;    
        else if(pageNumber > (listSize/queryLimit)+1)
            pageNumber = (listSize/queryLimit)+1;
        
        // set offset according to pageNumber    
        if((pageNumber-1)*queryLimit < 0) offset = 0;
        else offset = (pageNumber-1)*queryLimit;    
        
        /* if pageNumber is 1 than deactive previous navigator
           else if pageNumber is o tha set the value of pageNumber is 1
           else if pageNumber is more than 1 active next navigator
        */
        if(pageNumber == 1) firstOff = false;
        else if(pageNumber == 0) pageNumber = 1;
        else if(pageNumber > 1) firstOff = true;
        
        // user enter last number of pagenumber than deactive next navigator
        if(Math.Mod(listSize,queryLimit) == 0){
            if(pageNumber == listSize/queryLimit) lastOff = false;
            else lastOff = true;
        }
        else{
            if(pageNumber == (listSize/queryLimit)+1) lastOff = false;
            else lastOff = true;
        }
    }
}


Visualforce Page:

<apex:page controller="PaginationController"> 
   <apex:form >     
      <apex:pageBlock id="pgb" title="Accounts Details">                    
<apex:actionStatus id="status">            
  <apex:facet name="start">                
 <div style="width: 1300px;">                                      
 <img src="/img/loading24.gif" style="vertical-align:middle;"/>                    
 <span style="margin-left: 10px; font-size:12px; font-weight: bold; color:#000000;">Please wait...</span> 
 </div>
 </apex:facet>        
</apex:actionStatus>        
<apex:actionFunction name="first" action="{!first}" status="status" reRender="pgb"/>        
<apex:actionFunction name="last" action="{!last}" status="status" reRender="pgb"/>                         
<apex:actionFunction name="next" action="{!next}" status="status" reRender="pgb"/>                           
<apex:actionFunction name="previous" action="{!previous}" status="status" reRender="pgb"/>        
<apex:actionFunction name="changeLimit" action=" {!changeLimit}" status="status" reRender="pgb"/>               
<apex:actionFunction name="callAction" action="{!pageNavigation}" status="status" reRender="pgb"/>        

         <div id="scroll" style="height: 300px; width: 1300px;overflow:auto;">           
<apex:pageBlockTable value="{!records}" var="con" id="pgbt">            
<apex:column value="{!con.FirstName}"/>                          
<apex:column value="{!con.LastName}"/>                            
<apex:column value="{!con.Account.Name}"/>                        
<apex:column value="{!con.Email}"/>                        
</apex:pageBlockTable>        
         </div> <br></br><br></br>        
<div style="width:750px;">            
<span>                
<apex:outputLabel value="{!recordInfo}"/>                        
<apex:selectList value="{!limits}" size="1" onchange="changeLimit();return false;"> 
<apex:selectOptions value="{!items}"/>                
</apex:selectList>            
</span>            

<span style="margin: 0 200px;">                
            <span style="margin: 0 3px;">                                      
<apex:outputPanel rendered="{!firstOff}">                        
<img src="/s.gif" onclick="first();return false;" style="cursor:pointer; cursor:hand; background- image:Url('/img/paginationArrows.gif');background-position: 0 1px;background-repeat: no-repeat;height:10px;width: 9px"/
</apex:outputPanel>
<apex:outputPanel rendered="{!(!firstOff)}">                        
<img src="/s.gif" style="cursor:text; background-image:url('/img/paginationArrows.gif');background-position: 0-10px;background-repeat: no-repeat;height: 10px;width: 9px;"/>                                       
</apex:outputPanel>                
             </span>                
<span style="margin: 0 3px;">                    
<apex:outputPanel rendered="{!firstOff}">                        
<img class="prev" src="/s.gif" onclick="previous();return false;" style="cursor:pointer; cursor:hand;background-image: url('/img/paginationArrows.gif'); background-position: -10px 1px;background-repeat: no-repeat;height: 10px;margin: 0; padding: 0;width: 9px;"/>                        
<apex:outputLabel value="Previous"  style="cursor:pointer; cursor:hand;" onclick="previous();return false;"/>
</apex:outputPanel>                    
<apex:outputPanel rendered="{!(!firstOff)}">                        
<img src="/s.gif" style="cursor:text; background-image: url('/img/paginationArrows.gif');background-position: -10px -10px;background-repeat: no-repeat;height: 10px;margin: 0;padding: 0;width: 9px;"/>                        
<apex:outputLabel value="Previous"  style="cursor:text; color: #A8A8A8"/>                    </apex:outputPanel>                </span>                
<span style="margin: 0 3px;">                    
<apex:outputPanel rendered="{!lastOff}">                        
<apex:outputLabel value="Next" style="cursor:pointer; cursor:hand;" onclick="next();return false;"/>     
<img src="/s.gif" onclick="next();return false;" style="cursor:pointer; cursor:hand; background-image: url('/img/paginationArrows.gif');background-position: -17px 1px; background-repeat: no-repeat;height: 10px; width: 9px;"/>                    
</apex:outputPanel>                    
<apex:outputPanel rendered="{!(!lastOff)}">                        
<apex:outputLabel value="Next" style="cursor:text; color: #A8A8A8"/>                        
<img src="/s.gif" style="cursor:text; background-image: url('/img/paginationArrows.gif');background-position: -17px -10px;background-repeat: no-repeat;height: 10px;width: 9px;"/>                    
</apex:outputPanel>                
</span>                
<span style="margin: 0 3px;">                    
<apex:outputPanel rendered="{!lastOff}">                        
<img src="/s.gif" onclick="last();return false;" style="cursor:pointer; cursor:hand; background-image: url('/img/paginationArrows.gif'); background-position: -27px 1px;background-repeat: no-repeat;height: 10px;width: 9px;"/>                    
</apex:outputPanel>                    
<apex:outputPanel rendered="{!(!lastOff)}">                        
<img src="/s.gif" style="cursor:text; background-image: url('/img/paginationArrows.gif');background-position: -27px -10px;background-repeat: no-repeat;height: 10px;width: 9px; "/>                    
</apex:outputPanel>                
</span>            
</span>            
<span style="margin: 0 -80px;">                
<apex:outputLabel value="Page"/>                
<apex:inputText id="pageNo" value="{!pageNumber}" style="width:20px; text-align:center" size="3" onkeydown="return runScript(event);"/>                
<apex:outputLabel value="of {!totalPage}"/>            
</span>        
</div>    
</apex:pageBlock>    
<script>        
function runScript(obj){            
//alert(obj.keyCode);            
if(obj.keyCode == 13){                
callAction();   // call action function                
return false;            
}
else if((obj.keyCode > 34 && obj.keyCode < 40) || (obj.keyCode > 95 && obj.keyCode < 106) || (obj.keyCode > 47 && obj.keyCode < 58) || obj.keyCode == 8 || obj.keyCode == 46 ){  
return true;            
}else{ 
return false;            
}       
}    
</script> 
</apex:form>
</apex:page>

Wednesday 21 November 2012

Multiple Objects in Pageblock table using Wrapper class

We can Display multiple objects fields in page block table using Wrapper classes.


Apex Controller:




public class tt
{
    public List<Account> accLst {get; set;}
    public List<Contact> conLst {get; set;}
    public List<MyWrapper> wrapper {get; set;}

    public tt()
    {
        accLst = [select id,name from account   ] ;
        conLst = [select id,name from contact   ] ;
        wrapper = new List<MyWrapper>() ;
        for(Integer i=0 ;i<20;i++)
            wrapper.add(new MyWrapper(accLst[i] , conLst[i])) ;
    }
    
    public class MyWrapper
    {
        public Account accRec {get; set;}
        public Contact conRec {get; set;}
        
        public MyWrapper(Account acc , Contact con)
        {
            accRec = acc ;
            conRec = con ;
        }
    }
}



VisualForce Page

<apex:page controller="tt">
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockTable value="{!wrapper}" var="wrap">
                <apex:column headerValue="Account Name" value="{!wrap.accRec.Name}"/>
                <!-- you can add related fields here   -->
                <apex:column headerValue="Contact Name" value="{!wrap.conRec.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Wednesday 7 November 2012

Simple Code to display Records in Page block Table

Apex Controller:



public class table { 
    public PageReference Export() {
        PageReference p = Page.RenderAsPDF1;
        p.setRedirect(true);
        return p;
        //return null;
        }
    //constructor
    public table() 
        {                        
        }             
    public List<Account> accounts 
    {
        get {
            if(accounts != null) 
                {
                return accounts;
                } 
            else 
                {
                accounts = [Select Id, Name,BillingState, Phone, Website from Account ];
                return accounts;
                }
            }
        set;
    }
}



Visualforce Page:

<apex:page sidebar="false" tabStyle="Account" controller="table" >
        <apex:form >
           <apex:sectionHeader title="Account Details"/>
          
           <apex:pageBlock title="All Account">
               <apex:pageBlockTable value="{!accounts}" var="acc">
                   <apex:column value="{!acc.Name}"/>
                   <apex:column value="{!acc.BillingState}"/>
                   <apex:column value="{!acc.Phone}"/>
                   <apex:column value="{!acc.Website}"/>
               </apex:pageBlockTable>
           </apex:pageBlock>
      </apex:form>
</apex:page>