How to use a hyperlink apex?

363    Asked by DavidEDWARDS in Salesforce , Asked on May 4, 2023

I have a requirement to get the Contacts of an object to the Description of an Account. For that i Created a VF page Section in Account Object and Linked one VF Page which has a button in it.

Whenever the user clicks the button, the Page will refresh and will get the Contacts links in the Description Field

Below is my VF page Code


    
        
    

Below is my Apex Class (Controller) Code
public with sharing class LookupMainController 
{
    public List cid;
    public String accountName {get; set;}
    public Id accountId {get; set;}
    public List contacts {get;set;}
    public LookupMainController(ApexPages.StandardController controller){
        accountId = controller.getId();
    }
    public PageReference callContact(){
        //Getting all the Contacts referred to an Account
        if(accountId != null){
            contacts = [select id,Name,FirstName,AccountId,LastName from Contact where AccountId =: accountId ORDER BY CreatedDate];
        }
        //Getting the Description field of Account to null
        List AccList = [select Id,Name,Description from Account where Id =: accountId];
        Map UpdateAcc = new Map();
        for(Account acc : AccList){
            acc.Description = '';
            updateAcc.put(acc.Id,acc);
        }
        if(UpdateAcc.size()>0)
        update UpdateAcc.values();
        //Putting the Contacts as Hyperlinks in Description Field
        for(Account acc : AccList){
            for(Contact c : contacts){
                acc.Description += System.URL.getSalesforceBaseUrl().toExternalForm()+'/'+c.Id+' ';
                UpdateAcc.put(acc.Id,acc);
            }
        }
        if(UpdateAcc.size()>0)
        Update UpdateAcc.values();
        //Putting the Pagereference just to reload the same Account Page
        Id acId;
        for(Account ac : AccList){
            acId = ac.Id;
        }
        PageReference pg = new PageReference('/'+acId);
        return pg;
    }
}
Now in the Output, I am getting the Description field as the Group of Links. But instead of Links i want to get both the Name and Link of a particular contact within a Description Field.
See the screenshot for Output i am getting
Is there any way to get the Contact Name and Link respectively?
Answered by Diya tomar

To use a hyperlink apex -


In your code you are preparing link itself since description is long text area field
Just iterate over contacts and add Name in description
Try like this.
//Putting the Contacts as Hyperlinks in Description Field
for(Account acc : AccList){
    for(Contact c : contacts){
        acc.Description += c.Name +' '+;
        UpdateAcc.put(acc.Id,acc);
    }
}


Your Answer

Interviews

Parent Categories