+ Start a Discussion
sumit suwalkasumit suwalka 

hyperlink on lookup of Account

Hi All,
         i am trying to make the lookup field a hyper link which show the Account clicking on it.
my controller  is given below:-
 ({

    doinit : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Record Number ', fieldName: 'linkName', type: 'url', 
             typeAttributes: {label: { fieldName: 'Name' }, target: '_blank'}},
            {label: 'Sister Account ', fieldName: 's1', type: 'url', 
             typeAttributes: {label: { fieldName: 'Sister_Company__c' },value:{fieldName: 'Sister_Company__c.Id'}, target: '_blank'}},
            {label: 'Sister Account ', fieldName: 's2', type: 'url', 
             typeAttributes: {label: { fieldName: ' Sister_Company2__c' },value:{fieldName: 's2'}, target: '_blank'}}

          
        ]);
        
        var action = component.get('c.fetchRecords');
        var recordId =  component.get("v.recordId");
        action.setParams({rid : recordId});
        action.setCallback(this, function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                var allValues = response.getReturnValue();
            
                  allValues.forEach(function(record){
                    record.linkName = '/'+record.Id;
                });
               allValues.forEach(function(record){
                    record.s1 = '/'+record.Sister_Company__r.Name;
                }); 
                 allValues.forEach(function(record){
                    record.s2 = '/'+record.Sister_Company2__r.Id;

                }); 
                console.log("allValues--->>> " + allValues);
                component.set('v.mydata', allValues);
            }
            else if(state === "ERROR") {
                var errors = response.getError();
                if(errors){
                    if(errors[0] && errors[0].message){
                        console.log("Error Message: " + errors[0].message);
                    }
                }
                else{
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    removeRow : function(component, event, helper){
        var selRows = event.getParam('selectedRows');
        
        // Remove the record from the table
        var rows = component.get('v.mydata');
        for (var i = 0; i<selRows.length; i++){    
            var rowIndex = rows.indexOf(selRows[i]);
            console.log('rowIndex---->>> ' + rowIndex);
            var r=rows.splice(rowIndex, 1);   
            
            console.log('rrr---->>> ' + JSON.stringify(r));
            component.set('v.mydata', rows);
         }
    }
 })     
                here the name is working as hyper link but on lookup i am getting ids as hyperlink not their name.
                i want lookup field (Sister_Company__c's name as hyper link.
                how to do it?
                Any suggestions?
Ajay K DubediAjay K Dubedi
Hi Sumit,
Use this instead of helighted code of your controller:

{label: 'Sister Account ', fieldName: 's1', type: 'url', 
             typeAttributes: {label: { fieldName: 'Name' }, target: '_blank'}}

allValues.forEach(function(record){
                    record.s1 = '/'+record.Id;
                }); 
Also, follow this link you will find the solution of your requirement:
http://www.infallibletechie.com/2018/07/how-to-hyperlink-record-in.html

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

Thanks,
Ajay Dubedi
sumit suwalkasumit suwalka
Hi Ajay , 
                 i tried this before and its giving me the name of the record object .not the name of parent object which is my requirement.
                 i want that the lookup should be clickable(Sister_Company__c).
                 Any suggestions?
Ajay K DubediAjay K Dubedi
Hi Sumit,
For your requirement follow this code and change code accordingly.
I am taking an example with Contact and Account object. As Account is the parent of Contact:
Controller:
<aura:component implements="force:appHostable" controller="AccountListController">
                
    <aura:attribute type="List" name="acctList"/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchAccounts}"/>
    
    <lightning:datatable data="{! v.acctList }" 
                         columns="{! v.mycolumns }" 
                         keyField="id"
                         hideCheckboxColumn="true"/>
    
</aura:component>

Helper:
({
    
    fetchAccounts : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Account Name', fieldName: 'linkName', type: 'url', 
            typeAttributes: {label: { fieldName: 'acountName' }, target: '_blank'}},
            {label: 'Contact LastName', fieldName: 'contactName', type: 'text'}
        ]);
        var action = component.get("c.fetchAccts");
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records = response.getReturnValue();
                records.forEach(function(record){
                    var temp = JSON.stringify(record);
                    var test = JSON.parse(temp);
                    record.linkName = '/'+test.acountId;
                });
                component.set("v.acctList", records);
            }
        });
        $A.enqueueAction(action);
    }
})
Handler class:
public class AccountListController {
    
    @AuraEnabled
    public static List < ContactWrapper > fetchAccts() {
        List<ContactWrapper> wrapContact = new List<ContactWrapper>();
        List<Contact> conList = [SELECT Id, LastName, AccountId FROM Contact];
        set<Id> Accset = new set<Id>();
        for (Contact con : conList)
        {
            if(con.AccountId != null) {
                Accset.add(con.AccountId);
            }
        }
        List<Account> accList = [SELECT Id, Name FROM Account where Id IN : Accset];
        
        try {
            
            for (Contact c : conList) {
                
                for (Account a : accList) {
                    
                    if (a.Id == c.AccountId) {
                        ContactWrapper wrapObj = new ContactWrapper();
                        wrapObj.contactName = c.LastName;
                        wrapObj.acountName = a.Name;
                        wrapObj.acountId = a.Id;
                        wrapContact.add(wrapObj);
                    }
                }
            }
            return wrapContact;
        }
        
        catch (Exception ex) {
            system.debug('Exception---ofLine--->' + ex.getLineNumber());
            system.debug('Exception---Message--->' + ex.getMessage());
            return null;
        }
        
    } 
    
    public class ContactWrapper {
        @AuraEnabled
        public String contactName {
            get;set;
        }
        @AuraEnabled
        public String acountName {
            get;set;
        }
        
        @AuraEnabled
        public String acountId {
            get;set;
        }
    }
}
Thanks,
Ajay Dubedi