+ Start a Discussion

How can I create a link to a record in a trigger?

I've created a trigger that sends out an email.  The merge fields to the custom object are hard coded into the trigger.  I would like to include in the email a link to the record where it's pulling the data for the merge fields.  Can someone provide some insight as to how I can accomplish this?

Shashikant SharmaShashikant Sharma

Please provide your code , still I will suggest you to use a  formula field and use it as hyperlink. Add this in your email template.


Please give me an example of how you would write this formula field.  I'm lost as to how I would go about writing it.  Just to reiterate, I just want to provide a web link back to the record in the email.   Here is my code, as you requested:




trigger SendEmail on Marketing_Request_Form__c (after insert) { 
    List<Marketing_Request_Form__c> requestList =  [select  Id,

                                                    from Marketing_Request_Form__c 

                                                    where Id in :Trigger.new order by CreatedDate];

    List<User> users = [select email from User where Id in ('00570000001XhHZ', '005S0000000jEWH', '00570000001FDB8', '00570000001FG0m') limit 4];
    String[] ccAddrs = new String[4];
    for(Integer i=0; i<users.size(); i++){
         ccAddrs[i] = users[i].email;
    // Send emails
    for(Marketing_Request_Form__c request: requestList){
       ID toId = request.SalesRepresentativeID__c;        
       User user = [select name, email from User where Id =: toId limit 1];
       OrgWideEmailAddress orgWideEmail = [select Id from OrgWideEmailAddress where DisplayName =: user.Name limit 1];
     String subject = 'Marketing Request Form - Category: ' + request.Category__c + '  Account Name:' + request.Account_on_Opportunity__c;
       String body =  '<html><body><table>' + 
                           '<tr><td>Date Sent: </td><td>' + request.CreatedDate.format('MM/dd/yyyy hh:mm:ss a') + '</td></tr>' +
                           '<tr><td>Account Name: </td><td>' + request.Account_on_Opportunity__c + '</td></tr>' +
                           '<tr><td>Brand Name: </td><td>' + request.Brand_on_Opportunity__c + '</td></tr>' +
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.targetObjectId = toId;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});