+ Start a Discussion
Molson94Molson94 

SendEmail: Invalid Field When Using Template Help!

Hi Everyone,

I am attempting to send templated email through SendMail method.
When i was testing individual mails for functionality it worked perfectly fine. Now that i have looped out multiple mails, and want to send from a list i am getting an error.

Here is the error in detail when i try to run the script:
Line: 57, Column: 1
System.EmailException: SendEmail failed. First exception on row 1; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []


Here is the script that i am trying to run:
 
// Promo/Contact Objects
        List<Promotion__c> PromoList = new List<Promotion__c>(); // list of promo/title
        List<Title_Contact__c> ContactList = new List<Title_Contact__c>(); // list of title/contact
        List<String> TitleList = new List<String> (); //list to use to bind title contacts and titles needed (used to manage record size out of Title_Contact__c)
        Map<String, String> MailingList = new Map <String, String> (); //List of Promo/Contact that need to be mailed
        // Mailer Objects
        OrgWideEmailAddress Orgid = [SELECT Id FROM OrgWideEmailAddress WHERE displayname = 'APub Promo Notify']; //select the correct send from address
        EmailTemplate template = [SELECT id FROM EmailTemplate WHERE developerName = 'Ebook_PBook_Deals']; //get the standard template to be used        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); //initialize the individual emails
        List<Messaging.SingleEmailMessage> emails2send = new List<Messaging.SingleEmailMessage>(); //list of 'mail' to be sent through sendMail
        List<List<String>> MailList = new List<List<String>>();
        
        ///////////////////Create the MailingList MAP
        
        PromoList = [SELECT id, Title__r.id FROM Promotion__c  WHERE Notification_Status__c = 'Queued']; //;AND Start_Date__c = NEXT_N_DAYS:1
        if (!PromoList.isEmpty()){ //check if we have promos to act on
            for (Promotion__c title : PromoList) { //loop through to isolate the titles needed
                TitleList.add(title.Title__r.id); // build the list of titles in the title list string
            }
            System.debug('We have ' + PromoList.size()+' promotions to act on.');
            ContactList = [SELECT Legal_Name__r.Id, Title__r.Id 
                           FROM Title_Contact__c 
                           WHERE Title__r.Id IN :TitleList 
                           AND Role__c IN ('Author', 'Agent')]; //grab the recipients from the needed titles and the specific roles
            
            for (Title_Contact__c tc : ContactList) { //loop through lists to find matches on each level
                for (Promotion__c p : PromoList){
                    if(tc.Title__r.id == p.Title__r.id) { // have a match? add it to the map
                        MailingList.put(tc.Legal_Name__r.id, p.id);
                        
                    }              
                }
            }
            System.debug(MailingList);
        }else{
            System.debug('No Promotions to act on.');
            //No promotions in PromoList to send on.
        }
        
        
        ///////////////////Use the MailingList to create the individual email, and add to emails2send
        //If we have people to mail, compile mails, if not dont send
        if (!MailingList.isEmpty()){
            for (String c : MailingList.keySet()) {
                System.debug(c);
                System.debug(MailingList.get(c));
                String Contact = c;
                String Promotion = MailingList.get(c);
                mail.setOrgWideEmailAddressId(Orgid.id); //from address
                mail.setTemplateId(template.id); //standard template
                mail.setWhatId(Promotion); // what promo to merge
                mail.setTargetObjectId(Contact); // Specify who the email should be sent to.
                
                emails2send.add(mail);
            }
            Messaging.sendEmail(emails2send);
        }

The error is referencing fields that i am not attempting to alter in the building of "mail".
Thoughts?

Thanks in advance!
 
Best Answer chosen by Molson94
Tejpal KumawatTejpal Kumawat
Hello Cold Molson,

Can you put below line after line 48 & remove from line 09
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

If this answers your question then hit Like and mark it as solution!

All Answers

Molson94Molson94
Once pasted into the forum, Line 57 is now Line 56.
Molson94Molson94
After removing the call to send, i ran the logs on the compiled emails2send list. It seems that the criteria being thrown in the error are being set to null.
 
Messaging.SingleEmailMessage[getBccAddresses=null;getCcAddresses=null;getCharset=null;getDocumentAttachments=null;getFileAttachments=null;getHtmlBody=null;getInReplyTo=null;getOrgWideEmailAddressId=**********;getPlainTextBody=null;getReferences=null;getTargetObjectId=**********;getTemplateId=**********;getToAddresses=null;getWhatId=**********;isUserMail=false;]
Still cannot find a solution.
 
Tejpal KumawatTejpal Kumawat
Hello Cold Molson,

Can you put below line after line 48 & remove from line 09
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

If this answers your question then hit Like and mark it as solution!
This was selected as the best answer
Tejpal KumawatTejpal Kumawat
Hello Cold Molson,

If not work then also remove line 49 from above code, that is :
mail.setOrgWideEmailAddressId(Orgid.id); //from address

If this answers your question then hit Like and mark it as solution!
Molson94Molson94
Moving "mail" below line 48 fixed it.
Tejpal: Would you be able to provide any further detail as to why that was causing the error?