+ Start a Discussion
Mohammed Ikram 7Mohammed Ikram 7 

Hi, I want to send the records link of any objects(Contact,Account etc) by an apex trigger in an email

how to insert the record link.

The trigger is below , please give the code to add the hyper link of the record

-------

trigger DocLink on Contact (before insert) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
  for (Contact myContact : Trigger.new) {
    if (myContact.Email != null && myContact.FirstName != null) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail =
      new Messaging.SingleEmailMessage();
   
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(myContact.Email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('ikrammohiuddin@gmail.com');
      mail.setSenderDisplayName('Mohammed Ikram Moinuddin');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('ikrammohiuddin@gmail.com');
      mail.setCcAddresses(ccTo);
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('Link of the Contact '+ myContact.FirstName);
       String body = 'Dear ' + myContact.FirstName + ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am good';
  
      body += 'to a persom. Please respond with ';
      body += 'your details.';
  
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}
Best Answer chosen by Mohammed Ikram 7
PawanKumarPawanKumar
add the following line in your email body where you want.

URL.getSalesforceBaseUrl().toExternalForm() + '/' + contact.Id

Please mark best if it helps you. Thanks in advance.

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
add the following line in your email body where you want.

URL.getSalesforceBaseUrl().toExternalForm() + '/' + contact.Id

Please mark best if it helps you. Thanks in advance.

Regards,
Pawan Kumar
This was selected as the best answer
Mohammed Ikram 7Mohammed Ikram 7
Hi Pawan,

Thanks for the help.

The link is like this

https://ap5.salesforce.com/0037F00000k49WlQAI

I want to send that as HREF

a href="'+URL.getSalesforceBaseUrl().toExternalForm() + '/' + myContact.id;'">'+Click the link+'</a>

But it is giving double quote "" error after href.


Please help

Ikram