1

I am trying to display contact list in the visualforce page and when user click on the contact or on the detail button VF page should transfer to the standard record detail page.

I am trying both ways using outputlink and button but either ways is not working. please help.

Visualforce

   <apex:page standardController="Contact" extensions="ContacSearch" showHeader="false" standardStylesheets="false" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">  
<apex:form >
<apex:pageBlock >

           <div  style="margin-right:10%;" >
             <apex:pageblocksection >
                 <apex:pageblocksectionitem >
                    <apex:outputText style="font-weight:600" value="Search Contact"/>
                 </apex:pageblocksectionitem>
             </apex:pageblocksection><br></br>
           </div>   

                 <tr>               
                   <td>First Name </td>             
                   <td> <apex:inputText value="{!firstname}" /></td>
                 </tr><br></br>

                 <tr>                
                   <td>Last Name </td>            
                   <td><apex:inputText value="{!lastname}" /></td>
                 </tr>
               <br></br> <br></br>

             <div  style="margin-left:5%;" >
                <apex:commandButton value="Search"  action="{!searchContacts}" reRender="contact-table" />
             </div>
               <br></br>

            <div  style="margin-right:70%;" >
             <apex:pageblocksection >
                 <apex:pageblocksectionitem >
                    <apex:outputText style="font-weight:900" value="Contact Details"/>
                 </apex:pageblocksectionitem>
             </apex:pageblocksection>
           </div><br></br> 
                <apex:pageBlock >
                <apex:pageBlockTable style="width:50%" id="contact-table" value="{!contacts}" var="c">

                    <apex:column style="width:100px" >                                
                       <apex:facet name="header">First Name</apex:facet>     
                         {!c.FirstName} 
                    </apex:column>
                    <apex:column style="width:100px" >

                    <apex:outputLink value="{!URLFOR($Action.Contact.View, c.Id)}">
                    {!c.LastName}
                    </apex:outputLink>

                      </apex:column>
                  <apex:column style="width:100px" >
                  <apex:facet name="header">Detail</apex:facet>
                     <apex:commandButton value="Goto DetailPage" action="{!gotoContactDetailPage}" reRender="form">
                     <apex:param name="contactid" value="{!c.Id}" assignTo="{!contactId}"/>
                     </apex:commandButton>

                 </apex:column>  
                </apex:pageBlockTable>

          </apex:pageBlock>



</apex:pageBlock>
</apex:form>
</apex:page>

Controller

public with sharing class ContacSearch{

    public ContacSearch(ApexPages.StandardController controller) { 
    }

    public List<Contact> contacts { get; set; }
    public String firstname { get; set; }
    public String lastname { get; set; }
    public string contactId {get; set;}

    public ContacSearch()
    {
        contacts = new List<Contact>();
    }

    public PageReference searchContacts()
    {
        contacts = [select Id, FirstName, LastName from Contact 
                    where FirstName = :firstname
                    and LastName = :lastname];
        return null;
    }
    public PageReference gotoContactDetailPage()
    {
        return new PageReference('/' + contactId);
    }
}
  • N.B. contacts should use private set; as the collection is non-updateable from the page – cropredy Jan 15 '17 at 19:14
0
  1. <apex:outputLink/> to navigate to contact details
<apex:outputLink value="{!URLFOR($Action.Contact.View, c.Id)}">
        {!c.LastName}
 </apex:outputLink>
  1. <apex:commandLink/> as button to navigate to Contact Detail page without the use of controller method:
<apex:commandLink action="{!URLFOR($Action.Contact.View, c.Id)}" 
value="Goto DetailPage" 
target="_blank" styleClass="btn" style="padding: 4px; text-decoration: none;"/>
  1. Navigation with the use of <apex:commandButton/> through Controller method:
<apex:commandButton value="Goto DetailPage" action="{!gotoContactDetailPage}" reRender="form">
    <apex:param name="contactid" value="{!c.Id}" assignTo="{!contactId}"/>
</apex:commandButton>


public PageReference gotoContactDetailPage()
{
    return (new ApexPages.StandardController (new Contact(Id=contactId))).view();
}
  • either way it is still not transferring to record detail page. Dont know what the problem is. – SFLearner Jan 14 '17 at 7:35
  • can you pls post your full visualforce code, so I can look into this – Santanu Boral Jan 14 '17 at 16:31
  • I updated the VF code please check. – SFLearner Jan 15 '17 at 9:08
  • I have taken your code at my DE and it is perfectly navigating – Santanu Boral Jan 15 '17 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.