+ Start a Discussion
SrishSrish 

Visualforce custom search page

Kindly provide a solution to the following : 

 

Details

I as a product manager require a custom search page for my salesforce CRM account. I don’t want to use the standard search functionality for this. Here is what I want to be done:

 

    1. On the VF page, create accustom “search” button and a checkbox with the name “limit to accounts I own”
    2. I should be able to enter and search on the following fields (these are the search criteria fields):
      1. Account name(standard field)
      2. Industry (standard field)
      3. Rating(standard field)
      4. Account  region(custom field)
      5. Account priority(custom filed)
      6. Account summary(text)
    3. The search functionality should be a logical OR of all the above combinations.
    4. The correct search results should be displayed ON THE SAME PAGE only after clicking the “submit” button.Display columns should have these fields: account name, industry, rating, account priority.
    5. If the checkbox “limit to accounts I Own” is checked before clicking on search the display only those accounts where the owner=current logged in user. 

I have developed the following code:

 

<apex:page Controller="AccountSearch">
<apex:form >
 
<apex:outputPanel layout="block">
<label for="checkbox"> Limit to Account I own: </label>
<input id="checkbox" type="checkbox"/>
</apex:outputPanel>
<apex:commandButton action="{!search}" value="Search"/>
 
<apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Submit" action="{!search}" 
                                      rerender="block"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
 <apex:actionStatus id="status" startText="requesting..."/>
<apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="a"
                               rendered="{!NOT(ISNULL(results))}">
                               
              <apex:column value="{!a.Name}"/>
              <apex:column value="{!a.Industry}"/>
              <apex:column value="{!a.Rating}"/>
              <apex:column value="{!a.Account_Region__c}"/>
              <apex:column value="{!a.Account_Priority__c}"/>
              <apex:column value="{!a.Account_Summary__c}"/>
   </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
__________________________________________________________________________________________
 
public class AccountSearch {
 
  String searchText;
   List<Account> results;
 
public String getSearchText() {
      return searchText;
   }
 
   public void setSearchText(String s) {
      searchText = s;
   }
 
   public List<Account> getResults() {
      return results;
   }
    public PageReference search() {
    results = (List<Account>)[FIND :searchText RETURNING Account(Name, Industry, Rating, Account_Region__c, Account_Priority__c, Account_Summary__c)][0];  
        return null;
    }
 
}
Please provide the necessary modifications
Magesh Mani YadavMagesh Mani Yadav
Hi Srish,

Try the below updated code
public class AccountSearch {
 
  String searchText;
   List<Account> results;
    public Boolean IsOwnerSpecific {get;set;} 
public String getSearchText() {
      return searchText;
   }
 
   public void setSearchText(String s) {
      searchText = s;
   }
 
   public List<Account> getResults() {
      return results;
   }
    public PageReference search() {
        if(IsOwnerSpecific){
            results = (List<Account>)[FIND :searchText RETURNING Account(Name, Industry, Rating /*,Account_Region__c, Account_Priority__c, Account_Summary__c*/ where ownerId=:UserInfo.getUserId()) ][0];
        }else{
            results = (List<Account>)[FIND :searchText RETURNING Account(Name, Industry, Rating /*,Account_Region__c, Account_Priority__c, Account_Summary__c*/)][0];
        }
         
        return null;
    }
 
}

VF Page
<apex:page Controller="AccountSearch">
<apex:form >
 
<apex:outputPanel layout="block">
<label for="checkbox"> Limit to Account I own: </label>
    <apex:inputCheckbox value="{!IsOwnerSpecific}" />
</apex:outputPanel>
<!--<apex:commandButton action="{!search}" value="Search"/>-->
 
<apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Submit" action="{!search}" 
                                      rerender="block"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
 <apex:actionStatus id="status" startText="requesting..."/>
<apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="a"
                               rendered="{!NOT(ISNULL(results))}">
                               
              <apex:column value="{!a.Name}"/>
              <apex:column value="{!a.Industry}"/>
              <apex:column value="{!a.Rating}"/>
              <!--<apex:column value="{!a.Account_Region__c}"/>
              <apex:column value="{!a.Account_Priority__c}"/>
              <apex:column value="{!a.Account_Summary__c}"/>-->
   </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


And one more thing i believe Picklist field search are not possible in SOSL. for more info follow this success link
https://success.salesforce.com/ideaview?id=08730000000BrnQAAS

 
Magesh Mani YadavMagesh Mani Yadav
Please uncomment the line numbers 19 and 21 and in VF page line number 29-31. I forgot to uncomment it as these fields are not used in my DEV org :)
sarvendra aeturu 10sarvendra aeturu 10
HI Magesh Mani Yadav

i have a requirement to display custom object attachments on a visualforce page with a search functioanlity(able to search by the document name) i can able to develope the visualforce page to display the attachments in a visualforce page. please help me in writing the search functionality.

below is the code 

VF page

<apex:page controller="KnowledgeBaseAttachment" showheader="false" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="2">
<apex:repeat value="{!listAttachment}" var="att">
<apex:pageBlockSectionItem >
<apex:outputLink value="{!URLFOR($Action.Attachment.Download, att.id)}" target="_blank">{!att.name}
</apex:outputLink>
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

controller code 

public class KnowledgeBaseAttachment {
    public List<Attachment> listAttachment {get; set;}
    public KnowledgeBaseAttachment() {
        listAttachment = new List<Attachment>();
        List<aeturu__Knowlegde_Base__c> lstKnowledgeBase = [Select Id, (Select Id, Name from Attachments)
                                                            from aeturu__Knowlegde_Base__c];
        for (aeturu__Knowlegde_Base__c obj : lstKnowledgeBase) {
            listAttachment.addAll(obj.Attachments);
        }
    }
}

any help is appriciated
Ragava reddyRagava reddy
Hi Sarvendra,

Please go through the below link it will helps for you,

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XhHkQAK

Thanks,
Raghavendra Reddy.D
Ragava reddyRagava reddy
Hi Srish,

You have to use below script you can dynamically search all the fields data,No need to apply any extra business logic for that,I think it will hepls for you.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
                <script>
                    $.noConflict();
                    jQuery(document).ready(function() {
                        jQuery.extend(jQuery.expr[":"], {
                            "containsIN": function(elem, i, match, array) {
                                return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
                            }
                        });

                        jQuery(document).on("keyup", ".searchbynameautsearch", function() {
                            jQuery('.list tbody tr').each(function() {
                                jQuery(this).css("display", "table-row");
                            })

                            var searchterm = jQuery(this).val();

                            if (searchterm.length > 0) {
                                var lmatch = jQuery('.list tbody tr').text();

                                var match = jQuery('.list tbody tr:containsIN("' + searchterm + '")');

                                var nomatch = jQuery('.list tbody tr:not(:containsIN("' + searchterm + '"))');

                                match.addClass('selected');

                                nomatch.css("display", "none");

                            } else {

                                jQuery('.list tbody tr').css("display", "");

                                jQuery('.list tbody tr').removeClass('selected');

                            }

                        });

                    });
                </script>

 Custom Search  :<apex:inputText value="{!recnamesearch}" styleClass="searchbynameautsearch serchfld" style="padding-left:5px;" />


Public string recnamesearch{get;set;}

Thanks,
Raghavendra Reddy.D