function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohan Raj 33Mohan Raj 33 

How to write a code for button New account for creating new account in standard controller with extension?

Hi, to all here I created the standard controller with extension for sorting,pagination, alpha bar navigation and edit and delete link for in one controller.Here now I try to the button to add to this controller but I failed on the action. So can any one to help me to done this function on here.
My controller,
public class StandardPaginationSorting {

    // Variables required for Sorting.
    public String soql {get;set;}
    public List <Account> CandidateList1 = New List <Account>();
    public String soqlsort {get;set;}
    public List <Account> CandidateList2 = New List <Account>();
    public List<Account> acc {get; set;}
    public List<Account> editdel {get; set;}

                // List used in to display the table in VF page.
                public List<Account> getCandidateList() {
                    // Passing the values of list to VF page.
                    return con.getRecords();
                    //all();
                }

                // instantiate the StandardSetController from a query locator
                public StandardPaginationSorting(ApexPages.StandardController controller){
                 con.getRecords();
                 all();
                 LoadData();
                }
                public ApexPages.StandardSetController con {
                    get {
                                                if(con == null) {
                                                                // String Query to have a list of cases for a respective End-user.
                                                                soql = 'SELECT Name, Website, BillingCountry, Phone, Type, Owner.Name FROM Account';

                                                                // Passing the String array to a list with Selected field sorting.
                                                                CandidateList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir ); 

                                                                // setting values of List in StandardSetController.
                                                                con = new ApexPages.StandardSetController(CandidateList1);

                                                                // sets the number of records in each page set
                                                                con.setPageSize(10);
                                                }
                                                return con;
        }
        set;
    }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
    public void first() {
        con.first();
    }

    // returns the last page of records
    public void last() {
        con.last();
    }

    // returns the previous page of records
    public void previous() {
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }

    // Method for Constructor is used for Test Class.
    public StandardPaginationSorting(){ 
        //all();     
    }

   //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = 'SELECT Id, Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account'; 

                                // Adding String array to a List array
                                CandidateList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable
                                con = new ApexPages.StandardSetController(CandidateList2);

                                // Set Page Size to 10
                                con.setPageSize(10);

    }

    // the current sort direction. defaults to asc
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc';} return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    
    ///For refresh function button
    public PageReference Refresh() {
    PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl()); 
    pageRef.setRedirect(true); 
    return pageRef;
    }
    
    //For the New Account Button
    String accnew = 'eeee';
    public PageReference NewAccount() {
    Pagereference Newacc = new PageReference(ApexPages.currentPage().getUrl());
    Newacc.setRedirect();
    return Newacc;
    }
    
    // the edit and delete link function
    
    ////used to get a hold of the account record selected for deletion
    public string selectedAccountId {get; set;}
    
    public void LoadData() {
        editdel = [SELECT Id, Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account];
        con = new ApexPages.StandardSetController(editdel);
        con.SetPageSize(10);
    }
    
    public void deleteAccount() {
        
        If(selectedAccountId == Null){
            system.debug('@@@@@@@@@@@@@@@@'+selectedAccountId);
            return;
        }
        
        //Find account record with in collection
        
        Account deleteacc = Null;
        For(Account a : editdel)
            If(a.Id == selectedAccountId){
                deleteacc = a;
                system.debug ('@@@@@@@@@@@@@@@@@@'+a);
                //Delete deleteacc;
                break;
            } 
            
            //If account record found to delete it  
            If(deleteacc != Null) {
               Delete deleteacc; 
            }
        //refresh the dataList
        LoadData();
    } 
    
    //the alpha bar navigation filter
    
    String A;
    public PageReference aaa() {
        A = 'a';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+A+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String B;
    public PageReference bbb() {
        B = 'b';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+B+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String C;
    public PageReference ccc() {
        C = 'c';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+C+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
       
    String D;
    public PageReference ddd() {
        D = 'd';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+D+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String E;
    public PageReference eee() {
        E = 'e';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+E+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String F;
    public PageReference fff() {
    F = 'f';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+F+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }

     String G;
    public PageReference ggg() {
        G = 'g';
        acc.clear();
        String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+G+'%\' ORDER BY Name';
        acc= Database.query(qry);
        con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String H;
    public PageReference hhh() {
     H = 'h';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+H+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String I;
    public PageReference iii() {
     I = 'i';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+I+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String J;
    public PageReference jjj() {
     J = 'j';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+J+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String K;
    public PageReference kkk() {
     K = 'k';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+K+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String L;
    public PageReference lll() {
     L = 'l';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+L+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String M;
    public PageReference mmm() {
    M = 'm';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+M+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String N;
    public PageReference nnn() {
     N = 'n';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+N+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String O;
    public PageReference ooo() {
    O = 'o';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+O+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    String P;
    public PageReference ppp() {
     P = 'p';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+P+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String Q;
    public PageReference qqq() {
     Q = 'q';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+Q+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String R;
    public PageReference rrr() {
     R = 'r';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+R+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String S;
    public PageReference sss() {
     S = 's';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+S+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String T;
    public PageReference ttt() {
     T = 't';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+T+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String U;
    public PageReference uuu() {
     U = 'u';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+U+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String V;
    public PageReference vvv() {
     V = 'v';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+V+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String W;
    public PageReference www() {
     W = 'w';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+W+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String X;
    public PageReference xxx() {
     X = 'x';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+X+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String Y;
    public PageReference yyy() {
     Y = 'y';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+Y+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    String Z;
    public PageReference zzz() {
     Z = 'z';
    acc.clear();
    String qry = 'SELECT  Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account WHERE Name LIKE \''+Z+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return Null;
    }
    
    public void all() {
        acc = [SELECT Name, Phone, Type, Owner.Name, BillingCountry, Website FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    
    

}

My Page,
<apex:page standardController="Account" extensions="StandardPaginationSorting" showHeader="false" sidebar="false" applyHtmlTag="true">    
    <!-- CSS added to display alternate row colors and Center align Text in PageblockTable -->
    <!--<style type="text/css">
        .oddrow{background-color: #00FFFF; } 
        .evenrow{background-color: #7FFFD4; } 
        .textalign{text-align:center; } 
    </style>--->

    <apex:form id="form">
        <!-- Tabstyle attribute is used to assign the color scheme to the pageblock.Here Candidate Object color scheme is used for the pageblock-->
        
        <apex:pageBlock id="pgblock" tabStyle="Account">
            <!--<apex:pageBlockSection title="Candidate Details -  Page #{!pageNumber}" columns="1" collapsible="false">-->   
                <!-- Rowclasses attribute is used to define different CSS classes for different rows. 
                     Rules attribute is used: borders drawn between cells in the page block table.
                     Title attribute will be used as a help text when a user hovers mouse over the Page Block table.
                     Styleclass, HeaderClass attributes are used to Center align Table Text in Page Block table --->
            <!--refresh the page and showing the same result of this refresh button--->
            
                                
 
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Refresh" action="{!Refresh}" />
               <!-- <apex:commandButton onclick="{!NewAccount()}" value="Newaccount"/>--->
            </apex:pageBlockButtons>
            
            <right>
            <apex:toolbar id="toolbar" width="100" height="10" style="background-color:White;background-image:none;">
            <right><apex:toolbarGroup itemSeparator="line">
                <apex:commandLink value="A" Action="{!aaa}"/>
                <apex:commandLink value="B" Action="{!bbb}"/>
                <apex:commandLink value="C" Action="{!ccc}"/>
                <apex:commandLink value="D" Action="{!ddd}"/>
                <apex:commandLink value="E" Action="{!eee}"/>
                <apex:commandLink value="F" Action="{!fff}"/>
                <apex:commandLink value="G" Action="{!ggg}"/>
                <apex:commandLink value="H" Action="{!hhh}"/>
                <apex:commandLink value="I" Action="{!iii}"/>
                <apex:commandLink value="J" Action="{!jjj}"/>
                <apex:commandLink value="K" Action="{!kkk}"/>
                <apex:commandLink value="L" Action="{!lll}"/>
                <apex:commandLink value="M" Action="{!mmm}"/>
                <apex:commandLink value="N" Action="{!nnn}"/>
                <apex:commandLink value="O" Action="{!ooo}"/>
                <apex:commandLink value="P" Action="{!ppp}"/>
                <apex:commandLink value="Q" Action="{!qqq}"/>
                <apex:commandLink value="R" Action="{!rrr}"/>
                <apex:commandLink value="S" Action="{!sss}"/>
                <apex:commandLink value="T" Action="{!ttt}"/>
                <apex:commandLink value="U" Action="{!uuu}"/>
                <apex:commandLink value="V" Action="{!vvv}"/>
                <apex:commandLink value="W" Action="{!www}"/>
                <apex:commandLink value="X" Action="{!xxx}"/>
                <apex:commandLink value="Y" Action="{!yyy}"/>
                <apex:commandLink value="Z" Action="{!zzz}"/>
                <apex:commandLink value="All" Action="{!all}"/>                
            </apex:toolbarGroup></right>
        </apex:toolbar>
        </right>
        
                <apex:pageBlockTable value="{!CandidateList}" var="CadList"  title="Click Column Header for Sorting"  styleclass="textalign" headerClass="textalign" >                                        
                    
                    <apex:column headerValue="Action">                        
                        <apex:outputLink value="/{!CadList.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
                        <a href="javascript:if (window.confirm('Are you sure?')) deleteAccount('{!CadList.Id}');" style="font-weight:bold">Del</a>                       
                    </apex:column>
                    
                    <apex:column >
                        <apex:facet name="header">
                           <apex:commandLink value="Name" action="{!toggleSort}" rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                           </apex:commandLink>
                        </apex:facet>
                        <apex:outputLink value="/{!CadList.Id}" target="_blank">{!CadList.Name}</apex:outputLink>
                        <!--<apex:outputField value="{!CadList.Name}"/>-->
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Phone" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Phone" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Phone}"/>
                    </apex:column>

                  <!--  <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Name" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Email" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Email}"/>
                    </apex:column>-->

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Type" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Type}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="OwnerName" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Owner.Name" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Owner.Name}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Website" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Website" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Website}"/>
                    </apex:column>
                    
                </apex:pageBlockTable>
            <!---</apex:pageBlockSection>---->

            <apex:panelGrid columns="6">
                <apex:commandLink action="{!first}">First</apex:commandlink>
                <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
                <apex:commandLink action="{!last}">Last</apex:commandlink>
                <apex:inputText value="{!pageNumber}"> PageNumber</apex:inputText> of 4
            </apex:panelGrid>

        </apex:pageBlock>
        <apex:actionFunction action="{!deleteAccount}" name="deleteAccount" reRender="form" >
           <apex:param name="accountid" value="" assignTo="{!selectedAccountId}"/>
        </apex:actionFunction>
    </apex:form>
</apex:page>
Here I want the button name: New button.
it's function : when I clicked the button it's redirect me to the account creation page if I click the cancel it's return to the current(previous)
output page. or it's returned the output page of the my visual page after creating the account.
For answer'sthanks in advance
Best Answer chosen by Mohan Raj 33
SandhyaSandhya (Salesforce Developers) 
Hi ,

If you want standard Account page, then you can use something like below
 
<apex:commandButton action="{!URLFOR($Action.Account.New)}"value="New Account"/>


Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya