+ Start a Discussion
Michael MMichael M 

Help creating hyperlink from trigger

Hello, we have a trigger on Lead that creates a Contact based on 2 fields from the Lead . After the Contact is inserted, we want to turn the Lead field that contains the name of the Contact to turn into a hyperlink with a link to that Contact record. I built a trigger, but it is not working, and I am not sure why not. Can someone please help. Here is the trigger:

trigger ContactsTrigger on Contact (after insert) {
     // add mobilephone to set and assign id variable
    Set<String> contactNumSet = new Set<String>();
    Id newConId;
    for (contact c : trigger.new){
        if (c.mobilephone != null){
            newConId = c.id;
            contactNumSet.add(c.mobilephone);
        }
    }
 //query related lead   
 List<Lead> refsToInvestigate = new List<Lead>();       
 List<Lead> refsToUpdate = new List<Lead>();  
 String nok1;
    refsToInvestigate = [select emergency_contact_phone__c from Lead where emergency_contact_phone__c = :contactNumSet];    
        for (Lead ref : refsToInvestigate){
                nok1 = ref.NextofKin__c; }
    
    for (Lead refer : refsToInvestigate){
                refer.nextofkin__c =  '<a href=https://centers--partial.lightning.force.com/'+newConId+'>'+nok1+ '</a>' ;
        refsToUpdate.add(refer);  } 
         update refsToUpdate;

}
Best Answer chosen by Michael M
SUCHARITA MONDALSUCHARITA MONDAL
Hi Michael,

Observations from your code.

1. Filtering SOQL with LeadIds for Uniqueness  (rather than filtering it with MobileNumber, there could be a possibility for duplicacy of records contains same mobile number)
2. I hope there is only one Contact associated with Lead as Lead's field will be updated with that Contact only. If not, then only latest/newly created Contact will be updated on Lead's field.


You can try with something below:

trigger ContactsTrigger on Contact (after insert) {
     // add mobilephone to set and assign id variable
    Set<Id> leadId = new Set<Id>();    
    for (contact c : trigger.new){
        if (c.leadId != null){
            leadId.add(c.leadId);            
        }
    }
 
 
 List<Lead>leadList = new List<Lead>([SELECT nextofkin__c,(SELECT Id,lastName FROM Contacts) FROM Lead WHERE Id IN:leadId]);
 List<Lead> leadToUpdate = new List<Lead>();
 for(Lead ld : leadList){
     for(Contact con : ld.Contacts){
         ld.nextofkin__c = HYPERLINK("/" & con.Id, con.lastName, '_self');
         leadToUpdate.add(ld);
     }
 }
 
 if(leadToUpdate.size()>0){
     update leadToUpdate;
 }


Share your thoughts,
Thanks,
Sucharita

All Answers

SUCHARITA MONDALSUCHARITA MONDAL
Hi Michael,

Observations from your code.

1. Filtering SOQL with LeadIds for Uniqueness  (rather than filtering it with MobileNumber, there could be a possibility for duplicacy of records contains same mobile number)
2. I hope there is only one Contact associated with Lead as Lead's field will be updated with that Contact only. If not, then only latest/newly created Contact will be updated on Lead's field.


You can try with something below:

trigger ContactsTrigger on Contact (after insert) {
     // add mobilephone to set and assign id variable
    Set<Id> leadId = new Set<Id>();    
    for (contact c : trigger.new){
        if (c.leadId != null){
            leadId.add(c.leadId);            
        }
    }
 
 
 List<Lead>leadList = new List<Lead>([SELECT nextofkin__c,(SELECT Id,lastName FROM Contacts) FROM Lead WHERE Id IN:leadId]);
 List<Lead> leadToUpdate = new List<Lead>();
 for(Lead ld : leadList){
     for(Contact con : ld.Contacts){
         ld.nextofkin__c = HYPERLINK("/" & con.Id, con.lastName, '_self');
         leadToUpdate.add(ld);
     }
 }
 
 if(leadToUpdate.size()>0){
     update leadToUpdate;
 }


Share your thoughts,
Thanks,
Sucharita
This was selected as the best answer
Michael MMichael M
Hi Sucharita,

Thank you so much for this idea. I am running into an error on the link with  HYPERLINK .

Here is the line as I wrote it:
   ld.nextofkin__c = HYPERLINK('/' + con.Id, con.lastName, '_self');

Here is the error on the bottom:
Method does not exist or incorrect signature: void HYPERLINK(String, String, String) from the type ContactsTrigger
SUCHARITA MONDALSUCHARITA MONDAL
Hi Michael,
Please check the below link:
https://help.salesforce.com/articleView?id=tips_for_using_hyperlink_formula_fields.htm&type=5

Thanks,
Sucharita
Michael MMichael M
Great thank you. Your trigger worked great, I just replaced the hyperlink line with :
        String fieldvalue = lead.NextofKin__c; 
            fieldValue = fieldValue.replaceAll('<[^>]+>',' ');
        ld.nextofkin__c = '<a href=https://centers--partial.lightning.force.com/'+con.id+'>'+fieldValue+ '</a>';
Michael MMichael M
Hi Sucharita,
We could actually have more than 1 Contact (we can have up to 3) associated with the Lead. E.g. Next of Kin 2 and Next of Kin 3. Is there any way to adjust the trigger to include those as well?