You need to sign in to do that
Don't have an account?

ApexPages class
Hi,
I originally had posted asking if ApexPages class fails if called in a schedule (non VF page) what can do. Well I found out that it does fail. Is there a built in method that I can do a condition check to confirm if I am executing a schedule and if I am, don't execute the message line.
I guess if there isn't a built in method then I have to try passing a value to my method to do the same thing. just trying not to do that if a built in method exists.
15:05:37:273 FATAL_ERROR System.FinalException: ApexPages.addMessage can only be called from a Visualforce page
public void SendEmail() { try { Messaging.reserveMassEmailCapacity(2); Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage(); mail.setTemplateId(getTemplateId('CFCT_OwnerLeadRepNoMatch')); // Specify the address used when the recipientCount reply to the email. mail.setReplyTo('SFDCsupport@carefirst.com'); // Specify the name used as the display name. mail.setSenderDisplayName('SFDC Support'); // Set to True if you want to BCC yourself on the email. mail.setBccSender(true); mail.setUseSignature(false); System.debug('zzz: ' + userids); mail.setTargetObjectIds(userIds); mail.setSaveAsActivity(false); Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail }); ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.INFO, '<font color="blue">Mass email has been sent</font>'); ApexPages.addMessage(myMsg); } catch( EmailException e ) { ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, '<font color="red">' + e.getMessage() + '</font>'); ApexPages.addMessage(myMsg); } catch( exception e ) { ApexPages.addMessages(e); } }
It appears that checking currentPage for null does the trick
if (Apexpages.currentPage() != null) {
ApexPages.addMesage(...);
}