0

I have done in the sorting, pagination and alphabar navigation and edit/delete link on the same page in standard controller/standardList controller. However, the delete link is failing to delete the record.

And also I have now try to provide the button name new account when I click it it's redirect to the account creation page.

Apex 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
    public PageReference NewAccount() {
    Pagereference Newacc = new PageReference(ApexPages.getUrl());
    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){
            return;
        }

        //Find account record with in collection
        Account deleteacc = Null;
        For(Account a : editdel)
            If(a.Id == selectedAccountId){
                deleteacc = 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);
    }



}

Visualforce

<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:commandButton value="" />
            </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>
3
  • You got to write functions for both New and Delete in your apex and related command buttons in VF page to have those functions. Have you tried writing code for both buttons? Nov 1, 2016 at 11:35
  • @C.Praveenkumar I have only write code for the button delete not new I have tried but I failed to that to write that in my controller.So that's why I tells the both issue. In here my edit button link worked perfectly but my delete link not worked and I don't know to how to write the function for button in create an new account also.
    – Raj
    Nov 1, 2016 at 11:38
  • @C.Praveenkumar Now the delete function is working well Please help me to how to solve the issue for the new account button I have tried on my controller but I don't know to how to done this.
    – Raj
    Nov 1, 2016 at 11:51

2 Answers 2

2

Either you can write a method in apex to redirect OR you can handle this in the visualforce page itself.

Cancel Button which will return to your Account List View Standard Page:

<apex:commandbutton value="New Account" onclick="window.location='/001/e?retURL=%2F001%2Fo'; return false;"/>

Cancel Button which will return you to your VF Page:

<apex:commandbutton value="New Account" onclick="window.location='/001/e?retURL=/apex/MyPage'; return false;"/>
2
  • thans for reply but if I use the your solution in like <apex:commandbutton value="TMC Member Checklist" onclick="window.location='/001/e?retURL=%2F%'; return false;"/> I got this error :URL No Longer Exists when I click the cancel on my account creation page.so please help to how I navigate to return my output page on the vf page when I click the cancel button on my account creation page.
    – Raj
    Nov 1, 2016 at 12:27
  • Please check the above link now, I have changed the url a bit Nov 1, 2016 at 12:39
1

It seems like you have declared a actionFunction where the name starts with lowercase "deleteAccount". And in page you are calling

<a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!CadList.Id}');" style="font-weight:bold">Del</a> 

I guess just changing "DeleteAccount" to "deleteAccount" should make it work, since JS is case sensitive.

2
  • I have changed Still it's not working avidev...
    – Raj
    Nov 1, 2016 at 11:42
  • yes Now it's correct(work) thanks for the reply.And Can you help me to write a function to the new account button to used for create a new account?
    – Raj
    Nov 1, 2016 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .