+ Start a Discussion
ThomasmThomasm 

trigger to send email

I have a trigger to send an email based off a custom object.   The trigger works and the email sends but in Gmail the images from the template do not show and in outlook nothing shows.  I have used this template in workflow rules and it works fine but will not work correctly in from my frigger.  Here is my code

trigger EmailNotify on Service_Call_Log__c (after insert) {
    for(service_call_log__C b: trigger.new){                          
              
                  EmailTemplate template = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'Case Created Service Call Log'];           
            
               String subject = template.Subject;
subject = subject.replace('{!Case.Account}', 'Your Case has been created');

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String htmlBody = template.HtmlValue;
htmlBody = htmlBody.replace('{!Service_Call_Log__c.Subject__c}', b.subject__C);
htmlBody = htmlBody.replace('{!Service_Call_Log__c.Description__c}', b.Description__c);
mail.setSubject(subject);
mail.setHtmlBody(htmlBody);
mail.setToAddresses(new String[] {b.web_email__C});             
               mail.setSenderDisplayName('SFDC Developer');
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                      
    }
}
Shri RajShri Raj
Check this example
Id templateId = [Select id, Name from EmailTemplate where DeveloperName =: strEmailTemplate Limit 1].Id;
            Id OWAId = [select id, Address, DisplayName from OrgWideEmailAddress where Address='cmtapplicationsupport@csaa.com'].Id;
            Messaging.SingleEmailMessage objMail = new Messaging.SingleEmailMessage();
            objMail.setTemplateID(templateId);
            objMail.setTargetObjectId('005i0000001CwHJ');
            objMail.setToAddresses(lstEmail);
            objMail.setOrgWideEmailAddressId(OWAId);
            objMail.setSaveAsActivity(false);
            try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { objMail });
            }
            Catch(Exception e)
            {
                ApexPages.addMessages(e);
            }