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
cl0s3rcl0s3r 

Account Search Page

Does anyone know where I can find the details of Account search page? I would like to create a custom controller and vf page to add more details and need a starting point.

 

cashworthcashworth

I think I have exactly what you need here. I created a custom account controller and a VF page to replicate the functionality of the Account Owner Report that allows for users to search across the hierarchy for accounts despite security rules but includes the fields we needed. I added in the ability to sort on the fields as well via an additional class and public method. Lastly, I added it as a link to our home page as a component.

 

Below is the Class and Page for this. Hope its what you are looking for.

 

Controller Class:

public class AccountOwnerReport {


Public String NullResult = 'Your search did not return any results.';

public String getSearchText() {
             return null;       
        }

   private List<Account> result = new List<Account>();
   private String searchText;

   public AccountOwnerReport(){
   }


    
   public List<Account> getResult() {return result;}
   
   public String sortField {get; set;}
   public String previousSortField {get; set;}

   public void setSearchText(String searchText) 
        { this.searchText = searchText; }

        public void search() { 
        String queryText = '%' + searchText + '%'; 
{ 
        
        result = [select Id, Name, Type, OwnerId ,Region__c, Opportunity_Count__c, CreatedDate,LastActivityDate from Account Where Name like :queryText 
        ORDER BY Account.Name Asc
        ];}
        
}


  public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
       
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(result,sortField,order);
        
        
    }
    
  static testmethod void AccountSearchTest() {
     
    //call the controller 
    AccountOwnerReport aor = new AccountOwnerReport();
     
    
    //Insert Test Records for Query
      Account a1 = new Account(Name = 'testco1', type = 'Prospect');
      Insert a1;
    
      Account a2 = new Account(Name = 'testco1', type = 'Prospect');
      Insert a2;
      
    //Inject Text String
      String searchtext = 'test';  
      
    //Call the functions  
      aor.getSearchText();
      aor.getResult();
      aor.setSearchText(searchtext);
      aor.search();
      aor.sortfield = null;
      aor.previousSortField = null;
      aor.SortField = 'type';
      aor.doSort();
      
      aor.previousSortField = 'type';     
      aor.doSort();
      
      aor.SortField = 'name';
      aor.doSort();
  }    

}

 

 

Sorter Class: (not my code)

public class superSort {

    /*This method takes 3 arguments, the List of objects to sort, the field to sort, 
    and the order, asc or desc*/
    
    public static void sortList(List<sObject> items, String sortField, String order){
        /*I must give credit where it is due as the sorting algorithm I am using is the 
        one supplied by Andrew Waite here: http://blog.sforce.com/sforce/2008/09/sorting-collect.html */
        
        Boolean isSortFieldReference = false;
        Map<Id,String> referenceName;
         
        /*Determine the type of the field that needs to be sorted, if it is a 
        reference we will want sort by the name of the related object, not the 
        ID itself*/
        if(items[0].getSObjectType().getDescribe().fields.getMap().get(sortField).getDescribe().getType().Name() == 'REFERENCE'){
            isSortFieldReference = true;
            referenceName = new Map<Id,String>();
            
            /*Determine the type of this object and populate the Id to Name map*/
            Set<Id> referenceIds = new Set<Id>();
            for(sObject s : items){
               referenceIds.add((Id)s.get(sortField));
            }
            
            String objectID = (String)items[0].get(sortField);
            String prefix = objectID.substring(0,3);
            String objectType;
            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
            for(Schema.SObjectType s : gd.values()){
                if(prefix == s.getDescribe().getKeyPrefix()){
                    objectType = s.getDescribe().Name;
                }
            }
            
            //Query the related objects for the name and populate the Id -> Name map
            String queryString = 'select Id, Name from ' + objectType + ' where ID IN :referenceIDs';
            for(sObject s : Database.query(queryString )){
                referenceName.put((Id)s.get('Id'),(String)s.get('Name'));
            }
        }
                
        /*Declare a list that will contain the sorted results. I think this is one of the 
        coolest parts of this method as the system will not let you declare a list of 
        sObjects (List<sObject> objects = new List<sObjects>();) but using a 
        wrapper class you can bypass this system limitation to create this type of list */
        List<cObject> resultList = new List<cObject>();
    
        //Create a map that can be used for sorting 
        Map<object, List<cObject>> objectMap = new Map<object, List<cObject>>();
        
        for(sObject ob : items){
            if(isSortFieldReference == false){
                if(objectMap.get(ob.get(sortField)) == null){ 
                    objectMap.put(ob.get(sortField), new List<cObject>()); 
                }
                cObject o = new cObject(ob);
                objectMap.get(ob.get(sortField)).add(o);
            }else{
                if(objectMap.get(referenceName.get((Id)ob.get(sortField))) == null){ 
                    objectMap.put(referenceName.get((Id)ob.get(sortField)), new List<cObject>()); 
                }
                cObject o = new cObject(ob);
                objectMap.get(referenceName.get((Id)ob.get(sortField))).add(o);
            }
        }
        
        //Sort the keys
        List<object> keys = new List<object>(objectMap.keySet());
        keys.sort();
        
        for(object key : keys){ 
            resultList.addAll(objectMap.get(key)); 
        }
        
        //Apply the sorted values to the source list
        items.clear();
        if(order.toLowerCase() == 'asc'){
            for(cObject ob : resultList){
                items.add(ob.obj);  
            }
        }else if(order.toLowerCase() == 'desc'){
            for(integer i = resultList.size()-1; i >= 0; i--){
                items.add(resultList[i].obj);   
            }
        }
    }
    
    public class cObject{
        sObject obj {get; set;}
        
        public cObject(sObject obj){
            this.obj = obj; 
        }
    }
    
    /*Some test methods that provide 100% coverage */
    public static testMethod void sortAscendingTest(){
        
        List<Opportunity> opps = new List<Opportunity>();
        for(integer i = 0; i<1000; i++){
            opps.add(new Opportunity(Name = 'test' + i, Amount = 1000 * Math.random()));
        }
        
        Test.startTest();
        Long start = system.currentTimeMillis();
        sortList(opps,'Amount','asc');
        system.debug(system.currentTimeMillis() - start);
        Test.stopTest();
        
        //Assert the list was sorted correctly
        Decimal assertValue = -1;
        for(Opportunity o : opps) {
            System.debug('Opp value: ' + o.amount);
            System.assert(assertValue <= o.amount);
            assertValue = o.amount;
        }  
    }
    
    public static testMethod void sortDescendingTest(){
        
        List<Opportunity> opps = new List<Opportunity>();
        for(integer i = 0; i<1000; i++){
            opps.add(new Opportunity(Name = 'test' + i, Amount = 1000 * Math.random()));
        }
        
        Test.startTest();
        sortList(opps,'Amount','desc');
        Test.stopTest();
        
        //Assert the list was sorted correctly
        Decimal assertValue = 1001;
        for(Opportunity o : opps) {
            System.debug('Opp value: ' + o.amount);
            System.assert(assertValue >= o.amount);
            assertValue = o.amount;
        }  
    }
}

 


Visualforce Page:

<apex:page cache="true" controller="AccountOwnerReport" tabStyle="Account" showHeader="true">

 


<apex:form id="AccountOwner">
 <apex:pageBlock title="Global Account Search">
 
 
     <p>
     Input a name or partial name of the account you are searching for. The fewer characters you include in the search the more results that will be returned.
     (Example: to search for Test Company Inc. simply include "test" in the search)
     </p>
 
 
 
 
 </apex:pageBlock>

   <apex:inputText id="searchBox" value="{!searchText}"/>   
   <apex:commandButton action="{!search}" value="Search" id="searchBtn"/> <p>For Internet Explorer users please insure you click on the search button</p>
    
     <apex:pageBlock title=""> 

          <apex:pageBlockTable value="{!result}" var="a" id="table">
                                               
               
            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.Name.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.Name}"/>
                </apex:column>

            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.Type.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.Type}"/>
                </apex:column>
                
                 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.Opportunity_Count__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Opportunity_Count__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.Opportunity_Count__c}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.OwnerId.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.OwnerId}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.Region__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Region__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.Region__c}"/>
                </apex:column>
                
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.LastActivityDate.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LastActivityDate" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.LastActivityDate}"/>
                </apex:column>    
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Account.Fields.CreatedDate.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!a.CreatedDate}"/>
                </apex:column>

            
        </apex:pageBlockTable>
        
   
  </apex:pageblock>
    
</apex:form>




</apex:page>

 

 

RadheshRadhesh

very useful! Thanks.