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
santoshasbesantoshasbe 

Visualforce Email Template in apex code.

I have created a visualforce email template which is related to a custom object and recipient type is user.  I am  using this template in apex code to send the email but it does not send any email while the same email template shows correct merge field etc in case of preview.

 

i am using the following code to send the email

 

Messaging.SingleEmailMessage mail = mail.setTemplateId(templates[0].Id);

 

//currently loggoed in user's Id, getting value from UserInfo.getUserId()

mail.setToAddresses(mail.setTargetObjectId(currentUser.Id);

mail.setWhatId(record.Id); //a custom object record id

mail.setSaveAsActivity(false);

mail.setReplyTo(senderEmail); //its valid email id

mail.setSenderDisplayName(senderDisplayName);

 

Messaging.sendEmail(

new Messaging.SingleEmailMessage[]{mail});

When i use debug log, it show a mail in email queue still its not sending any email.

so could any one tell me that where i am commiting a mistake??

LakshmanLakshman

Is it complete code?

 

mail.setTemplateID seems to be missing.

 

 

santoshasbesantoshasbe

Actually it was there in the original code but while posting the code here. i have commited some mistake.

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setTemplateId(templates[0].Id);

 

meanwhile i have tried to send email by workflow using the same Email Template and it was working so i dont understand that why it is happening??

Juan SpagnoliJuan Spagnoli

Why are you doing this?

 

mail.setToAddresses(mail.setTargetObjectId(currentUser.Id);

 setTargetObjectId is a void method. How were you able to compile that code??

samrat.1985@lntinfotechsamrat.1985@lntinfotech

Did you find any solution?

I am stuck in the same scenario