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
Lindsay RoseLindsay Rose 

SingleEmailMessage cannot pass params into methods (email.setSubject(subject) causes error)

I am trying to write a simple Apex class to send out an email to one of our administrators with a list of the "Peer To Peer" objects that have not been fully approved. I am planning on scheduling this class to run by implementing the Schedulable interface. My problem is that I am not able to get the methods of the SingleEmailMessage class to accept any parameters! The PeerToPeerHoldReportBuilder class simply provides the content for the email, so I am pretty sure the problem resides in the code below where I am actually creating the email:


Also, when I say that the method won't accept parameters, I mean that I cannot save my class if I call .setToAddresses(toAddresses), or .setHtmlBody(body), or .setSubject(subject). This is mind boggling for me! The error I am receiving states" Unexpected character, expected ')' "


public with sharing class PeerToPeerHoldReportBuilder
PeerToPeerHoldReporter reporter = new PeerToPeerHoldReporter();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String subject = 'Peer to Peer Report';
String body = reporter.ParseValues();
String[] toAddresses = new String[] {''};


List<Messaging.SendEmailResult> result = Messaging.sendEmail(new Messaging.Email[] { email } ,false);





Any help would be appreciated, this is the third time I have picked this task back up and each time I cannot continue working with it because the issues with the email classes methods not accepting parameters.


Best Answer chosen by Admin (Salesforce Developers) 
Dirk GronertDirk Gronert

Hi Lindsay,


is this your complete class PeerToPeerHoldReportBuilder??! do you have any methods? You can just ste a non static field in a non static methods and all your code is on class level ...