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
BeatofHeartBeatofHeart 

render issues in Pageblock Section

I am writing a code for contacts Custom search. i am able to do search for contacts sucessfully. but i am an issue with render on pageblock .

 

I have 3 issues.

 

1. i would like to disable "results " section until search is happened.

2. in below column " Name " is displaying as a column before search. i want to display only after search.

3. any suggestions test code.

 

appriciated folks your help.

 

----------
public with sharing class ContactSearch {

    public List<Contact> contactResults { get; set; }
    public string pageUrl {get; set;}
    public boolean newContact { get; set; }
  
    
    public ContactSearch (){
    	newContact = false;
    	  	
    }
      
    
    public PageReference newcontact() {
    /*PageReference pageUrl = new  PageReference('/003/e?retURL=%2F003%2Fo&RecordType=012i000000079tC&ent=Contact');
    pageUrl.setRedirect(true);
    return pageUrl;*/
    newContact = true;
    pageUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/003/e?retURL=%2F003%2Fo&RecordType=012i000000079tC&ent=Contact';
    return null;
     }
  
    public PageReference Back() {
    	        
    	        PageReference FCPage = new PageReference('/apex/Contact_FindStudent');
            	FCPage.setRedirect(true);
                return FCPage;
        
    }


    public String Back { get; set; }

    public PageReference Clear() {
        return null;
    }
     
  
    public String ClearText { get; set; }

    public String results { get; set; }

    public String searchText { get; set; }
    
    
    public String Name { get; set; }
     
    

    Public PageReference Search() {
    	
       //  List<List<Sobject>> results = [FIND :searchText IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)]; 
    	 try { 
    	 contactResults = ContactsService.findContactInSalesforce(searchText);
    	
    	 }
    	 catch (Exception e){} 
    	 return null;
    	 
    	//PageReference pageRef = new PageReference('/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+searchtext);
        //return pageRef ;
        
    }

}

 

<apex:page Controller="ContactSearch" sidebar="false" id="page">
<p>No peopleSoft Contact found.Please Search Salesforce below Using Name,Phone,email or Other identifying information.</p>
  <apex:includeScript value="/support/console/22.0/integration.js"/>
     <script type="text/javascript">
        function testOpenPrimaryTab() {
          
        
            //Open a new primary tab with the salesforce.com home page in it
            sforce.console.openPrimaryTab(null,'{!pageUrl}', true, 
                'newContact', openSuccess, 'newContact');
        }
        
        var openSuccess = function openSuccess(result){
            //Report whether opening the new tab was successful
            if (result.success == true) {
               // alert('Primary tab can be opened');
            } else {
                //alert('Primary tab cannot be opened');
            }
        };
        
     </script>
 <apex:form id="frm">
             <!--  <apex:image url="{!$Resource.Contactshome}" width="50" height="50"/>-->
            
     <apex:pageBlock mode="edit" id="block">
 
         <apex:pageBlockSection id="pbSectn">
            <apex:pageBlockSectionItem id="pbSitem" >
               <apex:outputLabel for="searchText">Search Salesforce Contacts</apex:outputLabel> 
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/> <br/><br/>
                  <apex:commandButton value="Search" action="{!search}" />
                   <apex:commandButton value="Clear" action="{!Clear}" onclick="clearValue()"/>
                   <apex:commandButton value="Back" action="{!Back}"/>
                                      
                                         
               </apex:panelGroup>
         </apex:pageBlockSectionItem>
        </apex:pageBlockSection>  
         <apex:pageBlockSection Title="Results" id="results" columns="1"  > 
          <apex:outputPanel style="text-align:center" layout="block" >
         <apex:commandbutton value="New Contact" action="{!newcontact}" style="float: centre;"  rendered="true" onclick="testOpenPrimaryTab():return false;"/>
          <script>
            testOpenPrimaryTab();
            return false
           </script>
         </apex:outputPanel>
          <apex:pageBlockTable value="{!contactResults}" var="c">
                  <apex:column headerValue="Name" > 
                       <apex:outputLink value="/{!c.Id} " >{!c.name}</apex:outputLink>
                  </apex:column>
                  <apex:column value="{!c.emplid__c}"> </apex:column> 
                  <apex:column value="{!c.Phone}" ></apex:column> 
                  <apex:column value="{!c.Email}" ></apex:column> 
                       
       </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type = "text/javascript">
   function clearValue()
    {
    document.getElementById('{!$Component.page:frm:block:pbSectn:pbSitem:searchText}').value = '';   
   
    return false;
   
 }
   </script>

</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Yeah, I completed test class for this class and also found a solution for your problems.

Following are the solutions for your problem. So, modify the code as highlighted in the following,

 

Controller:

public with sharing class ContactSearch {
   ........
    public boolean showResults {get;set;}  //include these lines
    
    Public PageReference Search() {    
       showResults = true;        //include these lines
       //  List<List<Sobject>> results = [FIND :searchText IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)];
         try {
         contactResults = ContactsService.findContactInSalesforce(searchText);
        
         }
         catch (Exception e){}
         return null;
         
        //PageReference pageRef = new PageReference('/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+searchtext);
        //return pageRef ;       
    }
}


VF page:


<apex:page Controller="ContactSearch" sidebar="false" id="page">
   .......
     <apex:outputpanel rendered="{!showResults == true}">   <!--keep the pageblock section inside outputpanel to render-->
  
         <apex:pageBlockSection Title="Results" id="results" columns="1" > 
          <apex:outputPanel style="text-align:center" layout="block" >
         <apex:commandbutton value="New Contact" action="{!newcontact}" style="float: centre;"  rendered="true" onclick="testOpenPrimaryTab():return false;"/>
          <script>
            testOpenPrimaryTab();
            return false
           </script>
         </apex:outputPanel>
          <apex:pageBlockTable value="{!contactResults}" var="c">
                  <apex:column headerValue="Name" >
                       <apex:outputLink value="/{!c.Id} " >{!c.name}</apex:outputLink>
                  </apex:column>
                  <apex:column value="{!c.emplid__c}"> </apex:column>
                  <apex:column value="{!c.Phone}" ></apex:column>
                  <apex:column value="{!c.Email}" ></apex:column>
                       
       </apex:pageBlockTable>
   </apex:pageBlockSection>
   </apex:outputpanel>    <!-- end of the outputpanel-->

</apex:pageBlock>
</apex:form>
<script type = "text/javascript">
   function clearValue()
    {
    document.getElementById('{!$Component.page:frm:block:pbSectn:pbSitem:searchText}').value = '';   
   
    return false;
   
 }
   </script>

</apex:page>

 

Testclass:

@isTest(SeeAllData = true)

public class TEST_ContactSearch{
   
    public static testMethod void test2(){
        Contact c = new Contact(LastName = 'test');  
        insert c;
                
        ContactSearch cs = new ContactSearch();        
        cs.newcontact();
        cs.Back();
        cs.Clear();
        cs.searchText = c.LastName;
        ContactsService cse = new ContactsService();
        ContactsService.findContactInSalesforce(cs.searchText);
        cs.Search();
        
    }
}

 

If you follows this test class you will get 96% code coverage for Contact Search class and 100% for ContactService

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

 

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi BeatofHeart,

 

For rendering the block or form or fields in Vfpage, you need to use the boolean value in the controller. Cn you provide the class that is refered in the function Search() as highlighted below?

 

Controller:

Public PageReference Search() {       
       //  List<List<Sobject>> results = [FIND :searchText IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)];
         try {
         contactResults = ContactsService.findContactInSalesforce(searchText);   //Here you are  using other class to searhc the value write?        
         }
         catch (Exception e){}
         return null;        
        //PageReference pageRef = new PageReference('/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+searchtext);
        //return pageRef ;       
    }

 

In the above function of the controller ContactSearch, you are using a line highlighted above. Can you provide that class? So that it will be easier to setup the render option.

BeatofHeartBeatofHeart

Thanks ,, i got the solution.

 

Can you help me on the test code. i got the solution. how to use boolean.  thanks below is the controller class.

---------

public static List<Contact> findContactInSalesforce(String searchText)
    {
        List<List<Sobject>> results = [FIND :searchText+'*' IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email,emplid__c)];
        return results[0];
    }

 

 

 

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Yeah, I completed test class for this class and also found a solution for your problems.

Following are the solutions for your problem. So, modify the code as highlighted in the following,

 

Controller:

public with sharing class ContactSearch {
   ........
    public boolean showResults {get;set;}  //include these lines
    
    Public PageReference Search() {    
       showResults = true;        //include these lines
       //  List<List<Sobject>> results = [FIND :searchText IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)];
         try {
         contactResults = ContactsService.findContactInSalesforce(searchText);
        
         }
         catch (Exception e){}
         return null;
         
        //PageReference pageRef = new PageReference('/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+searchtext);
        //return pageRef ;       
    }
}


VF page:


<apex:page Controller="ContactSearch" sidebar="false" id="page">
   .......
     <apex:outputpanel rendered="{!showResults == true}">   <!--keep the pageblock section inside outputpanel to render-->
  
         <apex:pageBlockSection Title="Results" id="results" columns="1" > 
          <apex:outputPanel style="text-align:center" layout="block" >
         <apex:commandbutton value="New Contact" action="{!newcontact}" style="float: centre;"  rendered="true" onclick="testOpenPrimaryTab():return false;"/>
          <script>
            testOpenPrimaryTab();
            return false
           </script>
         </apex:outputPanel>
          <apex:pageBlockTable value="{!contactResults}" var="c">
                  <apex:column headerValue="Name" >
                       <apex:outputLink value="/{!c.Id} " >{!c.name}</apex:outputLink>
                  </apex:column>
                  <apex:column value="{!c.emplid__c}"> </apex:column>
                  <apex:column value="{!c.Phone}" ></apex:column>
                  <apex:column value="{!c.Email}" ></apex:column>
                       
       </apex:pageBlockTable>
   </apex:pageBlockSection>
   </apex:outputpanel>    <!-- end of the outputpanel-->

</apex:pageBlock>
</apex:form>
<script type = "text/javascript">
   function clearValue()
    {
    document.getElementById('{!$Component.page:frm:block:pbSectn:pbSitem:searchText}').value = '';   
   
    return false;
   
 }
   </script>

</apex:page>

 

Testclass:

@isTest(SeeAllData = true)

public class TEST_ContactSearch{
   
    public static testMethod void test2(){
        Contact c = new Contact(LastName = 'test');  
        insert c;
                
        ContactSearch cs = new ContactSearch();        
        cs.newcontact();
        cs.Back();
        cs.Clear();
        cs.searchText = c.LastName;
        ContactsService cse = new ContactsService();
        ContactsService.findContactInSalesforce(cs.searchText);
        cs.Search();
        
    }
}

 

If you follows this test class you will get 96% code coverage for Contact Search class and 100% for ContactService

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

 

This was selected as the best answer
BeatofHeartBeatofHeart

Many Thanks!! it worked !

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Great, Have a good day...!