+ Start a Discussion
Gaurav AgnihotriGaurav Agnihotri 

Override New Button with VF page with does not have standard conroller

I created a VF page using a controller and not a standard controller. This VF paginate the Search result on Account. I have used Jeff's example 
http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/
<apex:page controller="CustomPaginationController" sidebar="false">
I want to override new button on Account with this VF page. Now, because I am not using Standard Controller on Account, VF page is not avaliable.

How can I override new button with this VF page?


 
Best Answer chosen by Gaurav Agnihotri
Amit Chaudhary 8Amit Chaudhary 8
Ohh Grt nice to see that you are using my blog :-
http://amitsalesforce.blogspot.in/search/label/Pagination

Same code is working fine for me :-
User-added image


Please try below code :- 
Page :-
<apex:page standardController="Account" extensions="CustomPaginationController" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!acc.Name}" label="Name"/> 
                <apex:inputText value="{!acc.Phone}" label="Phone" />
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock id="resultId" rendered="{!if(lstAccount != null && lstAccount.size > 0, true,false )}">
            <apex:pageBlockButtons >
                <div style="text-align:right"> 
                      Total Records Found: {!Con.resultSize}  
                          <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
                          <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
                          <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
                          <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
                          <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>           
                          <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
                          <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
                          <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> 
                          <img src="/s.gif" title="Last Page" alt="Last Page" class="last"/>         
                </div>
            </apex:pageBlockButtons>                
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!lstAccount}" var="acc" >
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Phone}"/>
                </apex:PageblockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class:-
public with sharing class CustomPaginationController 
{

    public CustomPaginationController(ApexPages.StandardController controller) {
       acc = new Account();
       lstAccount = new List<Account>();

    }

    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController ()
    {
    }
    public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
        else
        {
        }
       return null;
    }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}

Please let us know if this will help you

 

All Answers

shikher goelshikher goel
Well you can't override the standard Account New button like this. YOu can use extensions if you need extra logic like 
<apex:page standardcontroller="Account" extensions="CustomPaginationController" sidebar="false">

This will work in both the cases..

Let me know if that doesn't work.
 
Gaurav AgnihotriGaurav Agnihotri
I am getting error message adding standardcontroller
Visualforce Error
Help for this Page

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Search}' in component <apex:commandButton> in page searchandcreateaccount: Class.CustomPaginationController.Search: line 30, column 1

Class.CustomPaginationController.Search: line 30, column 1

Below is the class:
public with sharing class CustomPaginationController 
{

    public CustomPaginationController(ApexPages.StandardController controller) {

    }

    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController ()
    {
       acc = new Account();
       lstAccount = new List<Account>();
    }
    public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
       if(acc.ShippingCity != null && (acc.ShippingCity).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where ShippingCity like \''+acc.ShippingCity+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And ShippingCity like \''+acc.ShippingCity+'%\'' ;
           }
        }
      if(acc.ShippingCountry != null && (acc.ShippingCountry).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where ShippingCountry like \''+acc.ShippingCountry+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And ShippingCountry like \''+acc.ShippingCountry+'%\'' ;
           }
        }
    if(acc.Account_Type__c != null && (acc.Account_Type__c).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Account_Type__c like \''+acc.Account_Type__c+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Account_Type__c like \''+acc.Account_Type__c+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone,ShippingCity,ShippingCountry,Account_Type__c,Customer_Number__c from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(20);
        }
        else
        {
        }
       return null;
    }
       public PageReference createAccount(){
            PageReference newAccPage2 = new PageReference('/001/e');
            // do not longer override the creation action   
            newAccPage2.getParameters().put('nooverride', '1');
            newAccPage2.getParameters().put('acc2', acc.Name);
            newAccPage2.getParameters().put('acc10', acc.Phone);
            newAccPage2.setRedirect(true);
            return newAccPage2; 

   }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Gaurav Agnihotri,
Please try below code:-
public with sharing class CustomPaginationController 
{

    public CustomPaginationController(ApexPages.StandardController controller) {
       acc = new Account();
       lstAccount = new List<Account>();

    }

    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController ()
    {
    }
    public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
       if(acc.ShippingCity != null && (acc.ShippingCity).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where ShippingCity like \''+acc.ShippingCity+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And ShippingCity like \''+acc.ShippingCity+'%\'' ;
           }
        }
      if(acc.ShippingCountry != null && (acc.ShippingCountry).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where ShippingCountry like \''+acc.ShippingCountry+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And ShippingCountry like \''+acc.ShippingCountry+'%\'' ;
           }
        }
    if(acc.Account_Type__c != null && (acc.Account_Type__c).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Account_Type__c like \''+acc.Account_Type__c+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Account_Type__c like \''+acc.Account_Type__c+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone,ShippingCity,ShippingCountry,Account_Type__c,Customer_Number__c from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(20);
        }
        else
        {
        }
       return null;
    }
       public PageReference createAccount(){
            PageReference newAccPage2 = new PageReference('/001/e');
            // do not longer override the creation action   
            newAccPage2.getParameters().put('nooverride', '1');
            newAccPage2.getParameters().put('acc2', acc.Name);
            newAccPage2.getParameters().put('acc10', acc.Phone);
            newAccPage2.setRedirect(true);
            return newAccPage2; 

   }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}

Please let us know if this will help you
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
Its not working. Same errror
Amit Chaudhary 8Amit Chaudhary 8
Ohh Grt nice to see that you are using my blog :-
http://amitsalesforce.blogspot.in/search/label/Pagination

Same code is working fine for me :-
User-added image


Please try below code :- 
Page :-
<apex:page standardController="Account" extensions="CustomPaginationController" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!acc.Name}" label="Name"/> 
                <apex:inputText value="{!acc.Phone}" label="Phone" />
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock id="resultId" rendered="{!if(lstAccount != null && lstAccount.size > 0, true,false )}">
            <apex:pageBlockButtons >
                <div style="text-align:right"> 
                      Total Records Found: {!Con.resultSize}  
                          <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
                          <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
                          <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
                          <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
                          <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>           
                          <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
                          <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
                          <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> 
                          <img src="/s.gif" title="Last Page" alt="Last Page" class="last"/>         
                </div>
            </apex:pageBlockButtons>                
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!lstAccount}" var="acc" >
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Phone}"/>
                </apex:PageblockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class:-
public with sharing class CustomPaginationController 
{

    public CustomPaginationController(ApexPages.StandardController controller) {
       acc = new Account();
       lstAccount = new List<Account>();

    }

    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController ()
    {
    }
    public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
        else
        {
        }
       return null;
    }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}

Please let us know if this will help you

 
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
Amit, 
Thanks, your code seems to work. Thanks again for saving my sorry a**. I am a poor Siebel Architect demoted to SFDC developer.