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
PappuPappu 

Standard object as hyperlink field

Hi Guys,

I have created a data table for custom sobject(test__c) list view.
I used SOQL query to get all the required fields (some are standard fields - Account and contact name) related to test__C.

But the issue here is, when the user clicks the standard fields, they should be able to navigate the Account and contact name pages respectively. For that, I need to make these as hyperlink fileds.

I am not sure how to proceed for this. Any help is appreciated.

Thanks!!
 
Best Answer chosen by Pappu
sfdcMonkey.comsfdcMonkey.com
use following code :
updated class 
public class Test {
    List<test__c> products;
    
    public List<test__c> getProducts() {
        products = [SELECT id,Name,Account_Name__c, Account_Name__r.Name,Contact_Name__c,Contact_Name__r.name FROM test__c];
        return products; 
    }
    
    
}
Account_Name__c will give you the id of record and Account_Name__r.Name gives you the name of lookup record

vf updated code :
<apex:page Controller="Test" >
    <apex:form id="form">
    <head >
        
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
            <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
            <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
            <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var contactTable = j$('[id$="contacttable"]').DataTable({
                    "columnDefs": [
                        { "width": "80px", "targets": -1 }
                        
                    ],
                    'bFilter': false,
                    "paging":   false
                    
                });
            });
             
           
            </script>
            <style>
                .colAlign
                {
                margin-left:50px;
                }
                 
            </style>
            <apex:slds />     
        </head>
        <body>
            <div class="slds-scope">
            <div class="slds-m-around_small">
            <div class="demo-only" style="padding: 0.5rem; background: rgb(22, 50, 92);">
            <div class="slds-text-color_inverse">
            <div class="demo-only slds-size_3-of-4">
            <div class="slds-media slds-media_center">
            <div class="slds-media__figure">
            <span class="slds-avatar slds-avatar_large">
           <img src="{!URLFOR($Resource.SLDS100,'images/avatar1.jpg')}"/>
            </span>
            </div>
            <div class="slds-media__body">
                <div class="slds-text-heading_medium">
                    <b>test</b>                    
                </div>
            
            </div>
            </div>
            </div>
            </div>
            </div>
            </div>
             
            <div class="slds-m-around_small">
              
                
            <table id="contacttable" class="display">
                <thead>
                    <tr>
                        
                        <th>Request Name</th>
                        <th>Account</th>
                        <th>Contact</th>
                        
                        
                    </tr>
                </thead>
                <tbody>
                
                    <apex:repeat value="{!products}"  var="contact" rows="25">
                        <tr>                          
                            <td class="slds-align_absolute-center">
                                <apex:outputLink value="/{!contact.id}">{!contact.Name}
                                </apex:outputLink>
                            </td>                                
                            <td><div class="slds-p-left_x-small">
                                <apex:outputLink value="/{!contact.Account_Name__c}"> {!contact.Account_Name__r.Name}</apex:outputLink></div></td>
                            <td><div class="slds-p-left_x-small"><apex:outputLink value="/{!contact.Contact_Name__c}">{!contact.Contact_Name__r.Name}</apex:outputLink></div></td>
                            
                        </tr>
                    </apex:repeat>
                   </tbody>
             </table></div>
            </div></body> 
       
        </apex:form>
   </apex:page>

Thanks let us know if it helps you and close your query with best answer if you got your solution
sfdcMonkey.com

All Answers

PappuPappu
Sure !!

Apex Class:

public class Test {
    List<test__c> products;
    
   public List<test__c> getProducts() {
        products = [SELECT id,Name,Account_Name__c,Contact_Name__c FROM test__c];
        return products;
                                    }
    
    
}

VF Page:

<apex:page Controller="Test" >
    <apex:form id="form">
    <head >
        
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
            <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
            <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
            <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var contactTable = j$('[id$="contacttable"]').DataTable({
                    "columnDefs": [
                        { "width": "80px", "targets": -1 }
                        
                    ],
                    'bFilter': false,
                    "paging":   false
                    
                });
            });
            
           
            </script>
            <style>
                .colAlign
                {
                margin-left:50px;
                }
                 
            </style>
            <apex:slds />     
        </head>
        <body><div class="slds-scope">
            <div class="slds-m-around_small">
            <div class="demo-only" style="padding: 0.5rem; background: rgb(22, 50, 92);">
            <div class="slds-text-color_inverse">
            <div class="demo-only slds-size_3-of-4">
            <div class="slds-media slds-media_center">
            <div class="slds-media__figure">
            <span class="slds-avatar slds-avatar_large">
            <img src="{!URLFOR($Resource.SLDS100,'images/avatar1.jpg')}"/>
            </span>
            </div>
            <div class="slds-media__body">
                <div class="slds-text-heading_medium">
                    <b>test</b>                    
                </div>
            
            </div>
            </div>
            </div>
            </div>
            </div>
            </div>
             
            <div class="slds-m-around_small">
              
                
            <table id="contacttable" class="display">
                <thead>
                    <tr>
                        
                        <th>Request Name</th>
                        <th>Account</th>
                        <th>Contact</th>
                        
                        
                    </tr>
                </thead>
                <tbody>
                
                    <apex:repeat value="{!products}"  var="contact" rows="25">
                        <tr>                          
                               <td class="slds-align_absolute-center">
                                <apex:outputLink value="/{!contact.id}">{!contact.Name}
                                </apex:outputLink>
                                </td>                                
                                <td><div class="slds-p-left_x-small">
                                   {!contact.Account_Name__c}/div></td>
                                <td><div class="slds-p-left_x-small">{!contact.Contact_Name__c}</div></td>
                                
                        </tr>
                        </apex:repeat>
                   </tbody>
             </table></div>
            </div></body> 
       
        </apex:form>
   </apex:page>
sfdcMonkey.comsfdcMonkey.com
use following code :
updated class 
public class Test {
    List<test__c> products;
    
    public List<test__c> getProducts() {
        products = [SELECT id,Name,Account_Name__c, Account_Name__r.Name,Contact_Name__c,Contact_Name__r.name FROM test__c];
        return products; 
    }
    
    
}
Account_Name__c will give you the id of record and Account_Name__r.Name gives you the name of lookup record

vf updated code :
<apex:page Controller="Test" >
    <apex:form id="form">
    <head >
        
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
            <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
            <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
            <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var contactTable = j$('[id$="contacttable"]').DataTable({
                    "columnDefs": [
                        { "width": "80px", "targets": -1 }
                        
                    ],
                    'bFilter': false,
                    "paging":   false
                    
                });
            });
             
           
            </script>
            <style>
                .colAlign
                {
                margin-left:50px;
                }
                 
            </style>
            <apex:slds />     
        </head>
        <body>
            <div class="slds-scope">
            <div class="slds-m-around_small">
            <div class="demo-only" style="padding: 0.5rem; background: rgb(22, 50, 92);">
            <div class="slds-text-color_inverse">
            <div class="demo-only slds-size_3-of-4">
            <div class="slds-media slds-media_center">
            <div class="slds-media__figure">
            <span class="slds-avatar slds-avatar_large">
           <img src="{!URLFOR($Resource.SLDS100,'images/avatar1.jpg')}"/>
            </span>
            </div>
            <div class="slds-media__body">
                <div class="slds-text-heading_medium">
                    <b>test</b>                    
                </div>
            
            </div>
            </div>
            </div>
            </div>
            </div>
            </div>
             
            <div class="slds-m-around_small">
              
                
            <table id="contacttable" class="display">
                <thead>
                    <tr>
                        
                        <th>Request Name</th>
                        <th>Account</th>
                        <th>Contact</th>
                        
                        
                    </tr>
                </thead>
                <tbody>
                
                    <apex:repeat value="{!products}"  var="contact" rows="25">
                        <tr>                          
                            <td class="slds-align_absolute-center">
                                <apex:outputLink value="/{!contact.id}">{!contact.Name}
                                </apex:outputLink>
                            </td>                                
                            <td><div class="slds-p-left_x-small">
                                <apex:outputLink value="/{!contact.Account_Name__c}"> {!contact.Account_Name__r.Name}</apex:outputLink></div></td>
                            <td><div class="slds-p-left_x-small"><apex:outputLink value="/{!contact.Contact_Name__c}">{!contact.Contact_Name__r.Name}</apex:outputLink></div></td>
                            
                        </tr>
                    </apex:repeat>
                   </tbody>
             </table></div>
            </div></body> 
       
        </apex:form>
   </apex:page>

Thanks let us know if it helps you and close your query with best answer if you got your solution
sfdcMonkey.com
This was selected as the best answer
PappuPappu
Not working as expected. 

Both - Account_Name__r.Name and  Account_Name__c returning the name of the Account in output link URL instead ID.

Output URL link:

https://xxxxxxxx.my.salesforce.com/B_AccountMaintance
PappuPappu
There is anyway I can retreive the ID of the account and pass it to VF page.

Will Hyperlink funtion works for this case?
PappuPappu
Thanks guys !!
Issue got resolved.
I used the same code as Piyush posted. Instead Account_Name__r.Name, I haved used Account_Name__r.Id in the command link button.