1

I'm Having problem with Passing WhatId while sending emails in apex class which invoked in process builder.

The process builder is enter image description here

invoked apex class is:

public class TrainingExpiring30Days {

  @InvocableMethod
  public static void sendEmail(List<Certification__c> certif) {

    String sourceAccountId = String.valueOf(certif[0].Id);

    List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();    
    Certification__c cert = [select id,Account__c from Certification__c Where id = :sourceAccountId];  
    System.debug('===Certificaiton: ' + cert);   
    List<Contact> contactsToEmail = [SELECT Id
                                     FROM   Contact
                                     WHERE  AccountId = :cert.Account__c AND CPP_Access__c INCLUDES ('Receive Training Expiry Notices')];
    System.debug('===Contacts:' + contactsToEmail );   
    Id templateId = [SELECT Id 
                     FROM   EmailTemplate 
                     WHERE  Name = 'CPP: Training expiring 30 days'].Id;                     
    string Es = 'Frank';
    string SenderName = UserInfo.getUserName();        

    system.debug('Sender Name is:'+ SenderName);        

    for(Contact con: contactsToEmail ){

      Messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
      email.setTemplateId(templateId);
      email.setTargetObjectId(con.Id);
      email.setSenderDisplayName(Es); 
      email.setWhatId(cert.id);
      emailsToSend.add(email);

    }
    Messaging.sendEmail(emailsToSend);
  }
}

I'm facing problem after run this calss:

SendEmail failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: a5Zq00000000HxuEAE: [WhatId]

Can anybody please help me to solve this issue?

0

I attach you the info related to the function setWhatId. As you can see in the end it can only be related this way to a certain type of standard objects, therefore Certification__c Id isn't valid as a parameter for this method. You should assign one of the valid Id types.

setWhatId(whatId)

If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data. Signature

public Void setWhatId(ID whatId)

Parameters

whatId Type: ID Return Value

Type: Void

Usage

The value must be one of the following types:

Account Asset Campaign Case Contract Opportunity Order Product Solution Custom

I assume you want to add some Certification fields on the template, then your options is to move by some means(apex,workflows...) that information from Certification to one of the valid objects (Account would be the most suitable one I guess, but I don't know the full scope of your custom Object) and then add that Id to the SetWhatId and take that field on the template.

  • 1
    Are you sure? WhatId can be any custom object – cropredy Apr 19 '19 at 15:30

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.