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
AngelikaAngelika 

SendEmail failed INVALID_SAVE_AS_ACTIVITY_FLAG saveAsActivity must be false

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I have a made a class that implements the schedulable interface. I am currently testing to make sure my code works. I've created a test method, but I'm not sure it works. I got this error email:

 

Sandbox

 

 

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_SAVE_AS_ACTIVITY_FLAG, saveAsActivity must be false when sending mail to users.: []

 

Class.AccountReviewSchedulerOtherObjectIDETest.sendEmail: line 15, column 1

Class.AccountReviewSchedulerOtherObjectIDETest.execute: line 5, column 1

 

 

Without a test method, I get no response from my scheduler (not an error email or the email I am trying to send.

 

Here is my Apex Scheduled Class:

 

global class AccountReviewSchedulerOtherObjectIDETest implements Schedulable{ 
global void execute (SchedulableContext ctx) 
{ 

sendEmail(); 

}
public void sendEmail()
{


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setTemplateId('00XF0000000LfE0'); 
mail.setTargetObjectId('005J0000000JWYx'); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}

public static testMethod void testschedule() {

Test.StartTest();
AccountReviewSchedulerOtherObjectID sh1 = new AccountReviewSchedulerOtherObjectID();

Test.stopTest();

}
}

 

 What does this error mean? Is my test class written the wrong way?

 

Thanks for all your help!

 

 

 

colemabcolemab

Try adding "mail.setSaveAsActivity(false);" to your code as you can't save emails to users as acitivites.

 

Sample code here.