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
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
Avish Shah 1Avish Shah 1
Please give me a solution
Details:

 Create a custom button ‘Send Message’ and place it on Account detail page.
 On click of button a VF Page should open. Here user will have ability to
select Contacts associated with this account to whom they wish to send the
message. Hence create a text space wherein user can search for contacts,
as soon as user starts to type show records matching that criteria so that
he can select the contact. (Similar to how you perform a google search, the
moment you typein the recommended results are available to select in
order to complete the text for search). Once a contact is selected, an email
field should be rendered which shows the email of selected contact. This
field value can be edited by sales team if they want.
 Now on the page a Text area should be available for user to type in
message. It should have a word counter and a word limit of 50.
 Show total word count remaining on page. Hence counter will start from
50 and keep on decreasing as soon as user starts typing in real time.
 When user crosses limit of 50 words and tries to save record an error
message should be displayed and user should not be able to send message.
 When counter reaches to ‘0’ stop to decrement the counter and show
word count remaining as ‘0’ and not as -1, -2 etc.
 A ‘Save’ button; on click of it an email will be sent to email address defined
by user along with message.
Deepali KulshresthaDeepali Kulshrestha
Hi Srish,

Please refer the below code for reference on Search Functionality.
VisualForce Page:

<apex:page controller="Searchname">
  <apex:form >
  <apex:PageBlock >
  <apex:PageBlockSection >
  <apex:PageBlockSectionItem >
  <apex:outputLabel > Name</apex:outputLabel>
  <apex:inputText value="{!name}"/>
  </apex:PageBlockSectionItem>
  <apex:commandButton value="Go" action="{!executeSearch}"/>
  </apex:PageBlockSection>
  <apex:PageBlockTable var="act" value="{!mycar}"> 
  <apex:column value="{!act.name}"/>  
  <apex:column value="{!act.Price__c}"/>
  </apex:PageBlockTable>
  </apex:PageBlock>
  <apex:PageBlock >
  <!--apex:commandButton value="Edit" action="{!edit}"/-->
  </apex:PageBlock>
  </apex:form>
</apex:page>

Apex Class:

public with sharing class Searchname {
     public String name { get; set;}
    public list<car__c> mycar { get; set; }
    public boolean searched{get;set;}
    public searchname() {
    searched=false;
    string namestr=ApexPages.currentPage().getParameters().get('name');
    if(null!=namestr) {
    name=namestr;
    //executeSearch();
    }
    }
    public PageReference executeSearch() {
      searched=true;
    System.debug('name' +name);
    string searchstr=('%'+name+'%');
    System.debug(searchstr);
   // accounts= new List<Account>();
    mycar=[select id,Name,price__c from car__c where name Like:searchstr limit 20];
    System.debug(mycar);
        return null;
    }  
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha