Getting Following error in the below code 23:27:13:030 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Invalid entity type for whatId.: [whatId, a09R000000AAPh5]

When i Print the InvoiceId 18 Char is printed but while sending in setWhatId 15 Char is sent.( Not sure this could be the Problem).

If i hard coded the 18 Char value in setWhatId it's working.

Can anyone help me on this this?

global class InvoiceDueNotification implements Database.batchable<sObject>{

global Database.QueryLocator start(Database.BatchableContext bc){
    String query = 'Select Id, Due_Date__c, Billing_Period_Name__c, Program_Name__c, Student_Family_Account__c, Student__c, Name, Amount__c, OwnerId from Invoice__c where Status__c =\'Open\'' ;
    system.debug('query' + query);
    return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC,  List<Invoice__c> scope)

    for(Invoice__c inv :(List<Invoice__c>) scope){
        Integer numberDaysDue = inv.Due_Date__c.daysBetween(Date.today());            
        system.debug('numberDaysDue' + numberDaysDue);
        If (numberDaysDue >= 5){
            Id TemplateId = NULL;
            TemplateId = [SELECT Id,Name FROM EmailTemplate WHERE DeveloperName =: 'Payment_Reminder'].Id;
            Account acc  = [SELECT Family_Emails__c,Name from Account where id=:inv.Student_Family_Account__c];
            string InvoiceId =  inv.Id;
            //send email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
            system.debug('inv' + InvoiceId);
            string[] to = new string[] {'test89@gmail.com'};  
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});



global void finish(Database.BatchableContext BC)

  • email.setWhatId((Id) InvoiceId); Not Working – user3214361 Nov 23 '19 at 18:36

Activities are not enabled for the Invoice object. Go to the Object Manager, and edit the properties of the object to allow activities.

Alternatively, use mail.setSaveAsActivity(false); so that no Tak is created.

As a side note, you need to create a list of emails, and send then atl at once, or you may reach governor limits.

Also, if you change your query, you can get rid of the if statement, and drastically improve your batch performance.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.