1

I am trying to send an VF Template using the Schedular Apex. when i run the code below is the error message received:

FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified, the plain text body, html body, subject, charset, and "treatBodiesAsTemplate" may not be specified : [].

What does that says.where i went wrong? Here is the Schedular code?

global class ScheduledUserEmail implements Schedulable {
    global void execute(SchedulableContext SC) {

List<Id> usr = new List<Id>();
            list<Id>emailUsr = new list<Id>();
        //set<Id> oppIds = new Set<Id>();
        List<Opportunity> clsopp = [Select Id,OwnerId from Opportunity where (CloseDate < TODAY OR CloseDate = NEXT_N_DAYS:7 ) and IsClosed = False and (StageName!='Closed/Won' or StageName!='Closed Lost' or StageName!='Closed Cancelled') and Owner.ProfileId =:'00eD0000001o9A0'];
        For(Opportunity opp : clsopp)
        {

            if(opp.OwnerId!=null)
            {
                //oppIds.add(opp.Id);
                usr.add(opp.OwnerId);
            }

        }

            for(User u:[Select Id,Name from User where IsActive = True and Id IN:usr])
            {

                if(u.id!=null)
                   emailUsr.add(u.id);
                system.debug('The Email of Users are:'+u.Name);
            }    


        //User u = [ Select Id,Email from User where Id IN:usr];
       // Opportunity o = [Select Id,Name from Opportunity where Id IN:oppIds limit 1];

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        for(User u :[Select id,Name from User where Id IN:emailUsr])
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
            mail.setTargetObjectId(u.id); 

            // The email template ID used for the email 
            mail.setTemplateId('00Xc0000000E7HD'); 

            //mail.setWhatId(o.Id); 
            mail.setBccSender(false); 
            mail.setUseSignature(false); 
            mail.setReplyTo('[email protected]'); 
            mail.setSenderDisplayName('Salesforce Support Team'); 
            mail.setSaveAsActivity(false); 
            mail.setSubject('Test Email');
            emails.add(mail);
        }

    Messaging.sendEmail(emails);

1 Answer 1

2

I got it fixed.

Just by removing the mail.setSubject('Test Email');

When an vf temaplte is used we should't include mail.setSubject();

The subject is taken from the vf template

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .