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
Ashu sharma 38Ashu sharma 38 

Account SOQL query

public class Apex_Account_search01 {
    
    
    public list<Account>  acclist   {set;get;}
    public string selected   {set;get;}
    public Apex_Account_search01(){
        
        acclist=[select id,name,phone from Account ];
    }
    
    public void sumbit(){
        
        string s=selected + '%';
        string query='select id,name,phone from Account where name like :s';
        acclist=Database.query(query);
        
    }
}
<apex:page controller="Apex_Account_search01">
<apex:form >
        <apex:inputText value="{!selected}">
            <apex:commandButton action="{!sumbit}" value="sumbit"/>
        </apex:inputText>
        <apex:pageBlock >
            
            <apex:pageBlockTable value="{!acclist}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.phone}"/>
                <apex:column value="{!a.id}"/>       
            </apex:pageBlockTable>
        </apex:pageBlock> 
        
    </apex:form>
</apex:page>

My requirmentis that I want to make a page just like Account listView whenever i click on A all the records start with A...and B,C....and all like that
Sai harsha 7Sai harsha 7
Hello Nitish Sharma

I have the code for you ,

Just try this one u will get the exact requirement.
 
VF Page Code:

<apex:page controller="filterrecordswithAlphabets">
    <apex:form >
        <apex:pageBlock >

            <table>
                <tr>
                <apex:repeat value="{!SerchAlpabet}" var="nn">
                    <td><apex:commandLink action="{!display}"  reRender="pg" >
                    <apex:param value="{!nn}" name="sss"  assignTo="{!SearchVar}"/> {!nn}</apex:commandLink></td>
                    </apex:repeat>
                </tr>
            </table>
            <apex:outputpanel id="pg">

                <div id="showdata">
                    <apex:pageBlockTable value="{!Showacc}" var="k">

                    <apex:column headerValue="Name" value="{!k.name}"/> 
                    <apex:column headerValue="Mobile Phone" value="{!k.Phone}"/> 
                    </apex:pageBlockTable>
                    </div>
                    <div id="nodata" style = "display:none;">
                    <p><b>No record by selected letter.</b></p>
                </div>  
                <script>
                    var sze='{!sizee}';
                    sz=parseInt(sze);
                    //  alert(sz);
                    if(sz>0)
                    {
                        document.getElementById('showdata').style.display="block";
                        document.getElementById('nodata').style.display="none";
                    }
                    else
                    {
                        document.getElementById('showdata').style.display="none";
                        document.getElementById('nodata').style.display="block";
                    }
                </script>   
            </apex:outputpanel>

        </apex:pageBlock>
    </apex:form>

</apex:page>




Apex Controller :

public class filterrecordswithAlphabets 
{
    public List<String> SerchAlpabet{get;set;}

    public String SearchVar{get;set;}

    public list<Account> acc{set;get;}

    public list<Account> Showacc{set;get;}

    public integer sizee{get;set;}

 

    public filterrecordswithAlphabets ()

    {

        SerchAlpabet=new List<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','All'};

        acc = new List<Account>();

        acc = [SELECT id,name,phone from Account order by Name];

        SearchVar='All';

        Showacc = new List<Account>();

        for(Account a : acc)        
        {

            if(SearchVar.equals('All'))

                Showacc.add(a);

            else

                if(a.Name.startsWith(SearchVar.toLowerCase())||a.Name.startsWith(SearchVar))

                Showacc.add(a);

        }

        sizee=Showacc.size();

         

    }

 

    public PageReference display()

    {

         Showacc.clear();

        for(Account a: acc)

        {

            if(SearchVar.equals('All'))

                Showacc.add(a);

            else

                if(a.Name.startsWith(SearchVar.toLowerCase())||a.Name.startsWith(SearchVar))

                {

                   Showacc.add(a);

         

                }  

            

        }

        sizee=Showacc.size();

        return null;
    }



}


Thanks & Regards
Srikanth
Salesforce Developer

 
Akshay_DhimanAkshay_Dhiman

Hi Nitish,

Please try the below code:

 

public class Apex_Account_search01 {
    public List<Account>  acclist{set;get;}
    public String    selected{set;get;}
 public Apex_Account_search01()
 {
        acclist=new List<Account>();
        acclist=[SELECT 
     id,
     name,
     phone
     FROM Account
    ];
    }
    public void sumbit()
 {
        String str=selected+'%';   //'%a' , 'a%' , '%a%'
        acclist=[select id,name,phone from Account where name like :str];
    }
}
<apex:page controller="Apex_Account_search01">
<apex:form>
        <apex:inputText value="{!selected}">
            <apex:commandButton action="{!sumbit}" value="sumbit"/>
        </apex:inputText>
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accList}" var="accObj">
                <apex:column value="{!accObj.name}"/>
                <apex:column value="{!accObj.phone}"/>
                <apex:column value="{!accObj.id}"/>       
            </apex:pageBlockTable>
        </apex:pageBlock> 
     </apex:form>
</apex:page>

Thanks
Akshay​
Ashu sharma 38Ashu sharma 38
Hello Akshay

I am getting an duplicate error.
"duplicate value found: <unknown> duplicates value on record with id: <unknown>"

And kindly tell me i want to use this query as  by using dynamic query.

Thank you for Your reply...

 
Akshay_DhimanAkshay_Dhiman
Hi Nitish 

Try for this code 

Without using the button.
// controller
 
public class Apex_Account_search01 {
    public list<Account>  acclist   {set;get;}
    public Apex_Account_search01(){
        
        acclist=[select id,name,phone from Account ];
    }
    
    public void sumbit(){
       string selected= ApexPages.currentPage().getParameters().get('record');
        System.debug(selected);
        string s=selected + '%';
        string query='select id,name,phone from Account where name like :s';
        acclist=Database.query(query);    
    }
}

///page 
<apex:page controller="Apex_Account_search01">
    <head>
        <script>
        function search()
        {
            var aa= document.getElementById("j_id0:frm:value").value;
           actionCall(aa);
        }
        </script>
    </head>
<apex:form id="frm">
    <apex:actionFunction name="actionCall" action="{!sumbit}" reRender="dd" oncomplete="true">
        <apex:param name="record" value=""/>
    </apex:actionFunction>
        <apex:inputText onkeyup="search()" id="value">
        </apex:inputText>
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accList}" var="accObj" id="dd">
                <apex:column value="{!accObj.name}"/>
                <apex:column value="{!accObj.phone}"/>
                <apex:column value="{!accObj.id}"/>       
            </apex:pageBlockTable>
        </apex:pageBlock> 
     </apex:form>
</apex:page>




Thanks.

Akshay