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
adrisseladrissel 

Home Page Sidebar Component

Hey all,

I want to implement a component in the sidebar of my home page for searching Contacts and Accounts.  I have a VF page with an associated controller that I want to use for this component (the search functionality is built in there).  However, I am experiencing an issue that I can't see how to fix.  When the search criteria is entered and the search button clicked, the component in the sidebar is what is refreshed with the resulting data, not the main page. 

How can I cause a sidebar component to refresh and display data on the main page?

Thanks!

Adam
fundooMentalfundooMental
Which is your main page? I mean where you want to show the search results?
adrisseladrissel
So, I want the search section and button in the left sidebar, but the results to populate in right section (where most normal content displays).  Essentially I want the clicking of the search button to refresh the entire page and display the results of the search in the main, right (wide) section.
fundooMentalfundooMental
So have you overriden the entire Home page? I mean the complete home page is now a custom Visualforce page?
adrisseladrissel
Well, you know how you can choose to show or hide the headers and/or sidebar with Visualforce.  I have a VF page I created that doesn't hide those, so it looks like a normal Salesforce screen.  What I want to happen is when someone enters a search term into the search section and clicks the Search button (both of which are in the left side bar) the data is sent to my controller and the entire page is directed to my VF page (which will render differently based on what data is entered).  Does that all makes sense?
fundooMentalfundooMental
What you can do. From your sidebar page pass the search string to the parameter key in the URL on click of the search button and recieve this parameter value in your other result displaying page and based upon the search string passed do the search logic in the result displaying page controller and then show the result.
adrisseladrissel
So essentially just have the button be a link? 

I think I had tried that before and the result was that the tiny little component section in the sidebar redirected to the link, not the whole browser page altogether.  I'll try it again and post the results here.
 
fundooMentalfundooMental
OK Great. I have given my suggestion based upon assumtion that the side-bar search is in one VF page and the result is shown in the other VF page ( I mean they both are not on the same page)
adrisseladrissel
Well, I have tried several different options at this point.  Using a Visualforce component, which allows me to use the controller, commandButton, etc...just rerenders the actual component in the narrow left sidebar.  Even if I hard link it to Google.com, as an example, it still only redirects that tiny little component section in the sidebar.  The only things I can get to redirect are components of the Custom Link or HTML type.  The HTML area type is lame because it no longer allows custom HTML (per a previous change in an earlier release). 

Are you able to get a VF component to actually rerender the main page section?
fundooMentalfundooMental
Can you put your code here? We can try our best to find out the solution
adrisseladrissel
Sure.  Here is the VF page that I link the VF component to:
<apex:page standardController="Contact" extensions="Five9ScreenPop" action="{!NumCases}"> 
    <apex:form>
         <div style="text-align: center;"> 
             <apex:inputText value="{!searchString}" label="Account/Contact Search"/>
             <apex:commandButton value="Search Contacts & Accounts" action="{!search}"/> 
         </div>
    </apex:form> 
</apex:page>


Here's the full controller code:
public with sharing class Five9ScreenPop{

    public Five9ScreenPop(ApexPages.StandardController controller) {}

    public String searchString {get;set;}
    private String oldSearchString {get;set;}
    public String query {get;set;}
    private String ani {get;set;}
    private String dnis {get;set;}
    private String shortAni {get;set;}
    private Id accId {get;set;}
    private Id aniId {get;set;}
    private Id conId {get;set;}
    public Id userId {get;set;}
    private String phone {get;set;}
    public List<Contact> cons {get;set;}
    public List<Case> conCases {get;set;} 
    public List<Case> accCases {get;set;}  
    public List<List<Case>> allFCases {get;set;} 
    public List<Case> allSCases {get;set;}  
    public PageReference pr {get;set;}
    public Integer numCases {get;set;}
    public Integer numCons {get;set;}
    public Integer numConts {get;set;}
    public Integer numAccs {get;set;}
    public Integer numAllSCases {get;set;}
    public Boolean noConCases {get;set;}
    public Boolean noAccCases {get;set;}
    public Boolean isSearch {get;set;}
    public String vfUrl {get;set;}
    public String instance {get;set;}
    public String baseUrl {get;set;}
    public List<List<Contact>> allContacts {get;set;}
    public List<List<sObject>> allAccsCons {get;set;}
    public List<Contact> contacts {get;set;}
    public List<Account> accounts {get;set;}
    public String retUrl {get;set;}
    private String prefix = Site.getPathPrefix();
    
    
    // METHOD IS RUN UPON PAGE LOAD OF FIVE9_SELECT_POP VF PAGE
    
    public PageReference NumCases(){
        
        noConCases = false;
        noAccCases = false;
        isSearch = false;
        baseUrl = getBaseUrl();  
        allFCases = getCases();      
                            
        if(allFCases == null){
            
            if(cons.size() == 1){
            
                aniId = cons[0].Id;
                accId = cons[0].AccountId;    
            
            
                // IF THERE ARE NO CASES, BUT AN ANI MATCH, REDIRECT TO NEW CASE PAGE (RECORD TYPE SELECTION SCREEN)
            
                numCases = 0;
                
                if(conId != null){
                
                    numCons = 0;
                    noConCases = true;
                    allSCases = null;
                }
                
                else{
                
                    pr = new PageReference('/setup/ui/recordtypeselect.jsp?ent=Case&retURL=%2F500%2Fo&save_new_url=%2F500%2Fe%3FretURL%3D%252F500%252Fo&def_contact_id='+aniId+'&def_account_id='+accId);
                }
            }
            
            else if(cons.size() > 1){
            
                numCons = cons.size();
            }
            
            else{
            
                // IF THERE ARE NO CASES, AND NO ANI MATCH, DISPLAY CONTACT/ACCOUNT SEARCH OPTION
                
                numCases = 0;
                numCons = 0;
            }
            
            return pr;
        }
        
        else if(allFCases[0].size() == 1){
        
            // IF THERE IS ONLY ONE CASE FOR THE CONTACT, REDIRECT TO THE CASE DETAIL PAGE
        
            numCases = 1;
            Id caseId = allFCases[0][0].Id;
            pr = new PageReference('/'+caseId);
            
            return pr;
        }
        
        else {
        
            // IF THERE IS MORE THAN ONE CASE FOR THE CONTACT THE FIVE9_SELECT_POP VF PAGE WILL SHOW A LIST OF OPEN CASES FOR THE CONTACT AND THE ACCOUNT
        
            numCases = allFCases.size();
        }
        
        return null;
    }
    
    
    public List<List<Case>> getCases(){    
    
        userId = ApexPages.currentPage().getParameters().get('id'); 
        ani = ApexPages.currentPage().getParameters().get('ANI');        
        conId = ApexPages.currentPage().getParameters().get('contactId');
        cons = new List<Contact>();
        
        if(userId != null){
        
            ani = '9999999999';
        }
                
            system.debug('----------------------------------------------------> Here is the ani value: '+ani );
        
        // IF CONID IS FOUND IN URL PULL ALL CONTACTS FOR CONTACT LIST ON VF PAGE
        
        if(conId != null){
        
            cons = [SELECT Name,Email,Phone,Id,AccountId,Account.Name FROM Contact WHERE Id =: conId]; 
        }
        
        else{
            
            if(ani == '8002331121'){
        
                // IF IT IS AN OUTBOUND CALL, DISPLAY THE CONTACT RECORD OF THE CONTACT BEING CALLED BASED ON DNIS
            
                dnis = ApexPages.currentPage().getParameters().get('DNIS');
            
                // CONVERT DNIS STRING INTO SALESFORCE-FORMATTED 10-DIGIT PHONE
                
                phone = '(' + dnis.substring(0, 3) + ') ' + dnis.substring(3, 6) + '-' + dnis.substring(6); 
            }
            
            else{   
        
                // CONVERT ANI STRING INTO SALESFORCE-FORMATTED 10-DIGIT PHONE
                
                phone = '(' + ani.substring(0, 3) + ') ' + ani.substring(3, 6) + '-' + ani.substring(6);  
            }
            
            // GET ALL CONTACTS WITH ASSOCIATED PHONE NUMBER       
             
            cons = [SELECT Name,Email,Phone,Id,AccountId,Account.Name FROM Contact WHERE Phone =: phone];   
        }
        
        // IF NO CONTACTS ARE FOUND IN INITIAL SEARCH, PERFORM MORE GENERALIZED SEARCH BASED ON LAST 4 DIGITS OF ANI
        
        if(cons.size() < 1){
        
            shortAni = ani.substring((ani.length()-7),ani.length()); 
        
            if(ani.length() > 10){ 
                      
                cons = [SELECT Name,Email,Phone,Id,AccountId,Account.Name FROM Contact WHERE Phone LIKE :'%' + shortAni + '%' AND Account.Name != null LIMIT 100];
            }
            
            if(cons.size() < 1){
            
                numCons = 0;
                return null;
            }
            
            else{
            
                numCons = cons.size();
                return null;
            }
        }
        
        // IF AN ANI MATCH IS MADE, GET ALL CASES ASSOCIATED WITH THAT CONTACT
        
        if(cons.size() >= 1){
        
            numCons = cons.size();
            
            // GET ALL CASES ASSOCIATED WITH CONTACT AND ITS PARENT ACCOUNT IF THERE IS ONLY ONE IN QUERY RESULTS
        
            if(cons.size() == 1){
                            
                conCases = [SELECT Id,Owner.Name,Account.Name,AccountId,Contact.Name,ContactId,CaseNumber,Status,CreatedDate,Priority,Severity_Level__c,Subject,RecordType.Name FROM Case WHERE ContactId =: cons[0].Id AND Status != 'Closed' ORDER BY CreatedDate DESC];
                accCases = [SELECT Id,Owner.Name,Account.Name,AccountId,Contact.Name,ContactId,CaseNumber,Status,CreatedDate,Priority,Severity_Level__c,Subject,RecordType.Name FROM Case WHERE AccountId =: cons[0].AccountId AND ContactId !=: cons[0].Id AND Status != 'Closed' ORDER BY CreatedDate DESC];
                                
                if(conCases.isEmpty() && accCases.isEmpty()){
                
                    noConCases = true;
                    noAccCases = true;
                                                            
                    return null;
                }
                
                else if(!conCases.isEmpty() && accCases.isEmpty()){
                
                    noConCases = false;
                    noAccCases = true;
                                                            
                    return null;                
                }
                
                else if(conCases.isEmpty() && !accCases.isEmpty()){
                
                    noConCases = true;
                    noAccCases = false;
                                                            
                    return null;          
                }
                
                else{    
                
                    noConCases = false;
                    noAccCases = false;                    
                    allFCases = new List<List<Case>>();
                    allFCases.add(conCases);
                    allFCases.add(accCases);            
                
                    return allFCases;
                }
            }
            
            // REDIRECT TO CONTACT LIST IN VF PAGE FOR MORE THAN ONE MATCHING PHONE NUMBER
            
            else{
            
                return null;
            }            
        }
        
        else{
            
            return null;
        }
    }
    
    public void search(){
        
        isSearch = true;
        contacts = new List<Contact>();
        accounts = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
        getCases();
        
        // GET ALL CONTACTS AND ACCOUNTS THAT MATCH THE SEARCH STRING ENTERED
        
        allAccsCons = [FIND :searchString IN ALL FIELDS RETURNING Contact(Name,Id,AccountId,Email,Phone,Account.Name),Account(Id,Name,Type,BillingCity,BillingState,Industry)];
                
        for(List<sObject> accsCons : allAccsCons){ 
        
            if(!accsCons.isEmpty()){
        
                for(sObject sobj : accsCons){
                
                    // ADD ACCOUNTS THAT HAVE THE SEARCHSTRING IN THEIR NAME
            
                    if(sobj.getSObjectType() == Account.sObjectType){
                    
                        Account acc = (Account)sobj;     
                        
                        if(acc != null && acc.Name.toLowerCase().contains(searchString.toLowerCase()) && !accIds.contains(acc.Id)){
                                     
                            accounts.add(acc);     
        
                            // SINCE ACCOUNT NAME FIELD ISN'T SEARCHABLE IN SOSL QUERY, USE SOQL TO GATHER ALL CONTACTS ON PARENT ACCOUNT
                                                  
                            List<Contact> conList = [SELECT Name,Id,AccountId,Email,Phone,Account.Name FROM Contact WHERE Account.Name =: acc.Name];                            
                            contacts.addAll(conList);
                        }
                        
                        accIds.add(acc.Id);
                    }
                    
                    // ADD CONTACTS THAT HAVE THE SEARCHSTRING IN THEIR NAME, ACCOUNT NAME, OR EMAIL ADDRESS
            
                    if(sobj.getSObjectType() == Contact.sObjectType){
                    
                        Contact con = (Contact)sobj;  
                
                        if(con != null && (con.Name.toLowerCase().contains(searchString.toLowerCase()) || (con.Account.Name != null && con.Account.Name.toLowerCase().contains(searchString.toLowerCase())) || (con.Email != null && con.Email.toLowerCase().contains(searchString.toLowerCase())))){
                            
                            contacts.add(con);
                        }
                    }
                }
            }
        }
            
        // IF CONTACTS ARE RETURNED BUT NO ACCOUNTS, FIND ACCOUNTS ASSOCIATED WITH RETURNED CONTACTS
        
        if(!contacts.isEmpty() && accounts.isEmpty()){
        
            List<Contact> allAccs = new List<Contact>([SELECT Account.Id,Account.Name,Account.Type,Account.BillingCity,Account.BillingState,Account.Industry FROM Contact WHERE Id IN :contacts]);
            
            for(Contact acct : allAccs){
            
                if(!accIds.contains(acct.AccountId)){
                
                    accounts.add(acct.Account);
                }
                        
                accIds.add(acct.AccountId);
            }               
        }
        
        numConts = contacts.size();
        numAccs = accounts.size();
            
        // GET ALL CASES ASSOCIATED WITH RESULTING CONTACTS
            
        if(contacts.size() > 0){
        
            allSCases = [SELECT Id,Owner.Name,Account.Name,AccountId,Contact.Name,ContactId,CaseNumber,Status,CreatedDate,Priority,Severity_Level__c,Subject,RecordType.Name FROM Case WHERE ContactId IN :contacts AND Status != 'Closed' ORDER BY CreatedDate DESC];
            numAllSCases = allSCases.size();
        }
        
        else{
        
            allSCases = null;
            numAllSCases = 0;
        }
                
        if(isSearch){
        
            numCons = 0;
            noConCases = false;
            noAccCases = false;
        }
    }
    
    public String getBaseUrl(){
        
        vfUrl = (Test.isRunningTest()) ? 'https://c.cs24.visual.force.com' : Url.getSalesforceBaseUrl().toExternalForm();
        Integer iIndex = vfUrl.indexOf('.visual');
        instance = vfUrl.substring((iIndex-4),(iIndex));
        baseUrl = 'https://'+instance+'.salesforce.com';
        
        return baseUrl;
    }
}


 
adrisseladrissel
I got it to work, kind of, using a Custom Link.  Basically the link in the narrow sidebar just goes to the VF page.  But, I really wanted to have the search section and button in the narrow sidebar itself.  It may not be possible.
Nikhil VodapallyNikhil Vodapally

Hi Adrissel,

I have a similar problem,

I added a VF page to the Sidebar which has the same functionality as the Advanced Search. The search works fine for me i.e. returns the resutls as a normal search would return, but when I log in a user the search resutls are displayed in the same VF page sidebar component. Do you know how to get around this?