Monday, June 20, 2016

FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: [WhatId]:

We have a trigger on some custom object and it would sent an email notification based on few changes to record.When we are sending an email with help of Messaging.SingleEmailMessage class and we are also using an email template so to populate the merge fields in template we are setting the whatId to custom object which full fills the merge fields in template but unfortunately we are end up with an below error



Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SummaryTrigger caused an unexpected exception, contact your administrator: SummaryTrigger: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: a0JJ000000A6TDWMA3: [WhatId]: Class.SummaryTrigHandler.sendEmailNotification: line 101, column 1

Root Cause:
Even after I am setting the correct WhatId I am getting the same error .This issue is not related to object WhatId it's actually related to Activity record whatId.
Because by default the setSaveAsActivity has a value true, which saves the emailing as an activity so If your WhatId custom object not allowed for activity tracking then you will receive this error.

Solution:

  • So if your facing the same issue please goto object detail page (i.e.,the object that your setting as part of whatId) and enable the activities for the object
  • Change in your code like do not save the activity by setting the setSaveAsActivity as false Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();  email.setSaveAsActivity(false)



Thanks for visiting....Enjoy!

No comments:

Post a Comment