HI friends for setWhatId i am unable to assign custom object record id. I am getting following exception. caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Only contracts, cases, products and opportunities allowed for whatIds.: []

That means we can not use custom object records as merge fields ?. Its really bad from salesforce.

whatId when using custom object


If you're sending the mass email to a list of contacts, what you describe is in the Apex documentation under MassEmailMessage Class:


Optional. If you specify a list of contacts for the targetObjectIds field, you can specify a list of whatIds as well. This helps to further ensure that merge fields in the template contain the correct data.


public Void setWhatIds(ID[] whatIds)



Type: ID[]

Return Value

Type: Void


The values must be one of the following types:

  • Contract
  • Case
  • Opportunity
  • Product
  • i must admit, I find this restriction to be inexplicable – cropredy Dec 31 '16 at 21:30
  • @cropredy I don't write the documentation, I just regurgitate it. ;) – crmprogdev Dec 31 '16 at 22:57
  • looks like its better to use SingleEmailMessage . Here for setWhatId(whatId) i can use custom objects too.The value must be one of the following types: Account Asset Campaign Case Contract Opportunity Order Product Solution Custom – mak k Jan 3 '17 at 5:50
  • @makk That would be correct. The only way to do what you want is to use SingleEmailMessaging methods, not MassEmail Methods – crmprogdev Jan 3 '17 at 12:54

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.