function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Murali KrishnaMurali Krishna 

Email through trigger

Can any body tell how to send an email through trigger in detail

souvik9086souvik9086

Hi,

 

Try this

 

Hi,

 

Try this

 

trigger EmailSendingToOwner on ObjectName(after update) {
EmailTemplate emailTemp = [select id,name from EmailTemplate].get(0);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(Trigger.new[0].OwnerId);
email.setTemplateId(emailTemp.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Yoganand GadekarYoganand Gadekar

You can go through This documentaion on outbound email sending through apex, howevere i think it is not a good practice to send emails from trigger(as there exists limit on how many emails you can send, i think you can only upto 10 of those). See this for governer limit Govener limits

You can use workflow if your logic is implementable through workflow.

 

Thanks,

Murali KrishnaMurali Krishna
hi souvik,
can you tell me the following error which i encountered while implemnting above code
Review all error messages below to correct your data.
Apex trigger EmailSendingToOwner caused an unexpected exception, contact your administrator: EmailSendingToOwner: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_SAVE_AS_ACTIVITY_FLAG, saveAsActivity must be false when sending mail to users.: []: Trigger.EmailSendingToOwner: line 6, column 1
souvik9086souvik9086

You can add the blue colored line and see what happens

 

trigger EmailSendingToOwner on ObjectName(after update) {
EmailTemplate emailTemp = [select id,name from EmailTemplate].get(0);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(Trigger.new[0].OwnerId);
email.setTemplateId(emailTemp.id);

email.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks