You need to sign in to do that
Don't have an account?
Weird SingleEmailMessage exception
Hey All,
I have a trigger set on a custom object called Feature_Setup__c. The details of what the object does isn't really relevant but the main purpose of the trigger is to send an email whenever one of these objects is created. I have a custom email template setup and I am setting the WhatId for the SingleEmailMessage to the Id of the Feature_Setup__c object. When I do this and then call the Messaging.SendEmail() method I get the following exception:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger New_Feature_Setup_Send_Email caused an unexpected exception, contact your administrator: New_Feature_Setup_Send_Email: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: []: Trigger.New_Feature_Setup_Send_Email: line 46, column 17
The exception is saying that WhatId is not available for sending emails to UserIds. I am not using the WhatId to send to a User it is set to the Id of my custom object. Anyone have any ideas on what this exception could mean? I'm at a loss.
Hi Ankit,
Thanks for the response. I checked the Field Level Security and everything is set to Visible. I did finally solve this issue but the weird thing is I didn't do anything differently. I think there was some kind of system issue with SF because all of a sudden the code worked without throwing an exception. Very weird but everything is working fine now.
All Answers
two steps to resolve this:
-SaveAsActivity must be explicitly set to False when you are using a User as a TargetObjectId.
- Make sure that you have the "Allow Activities" enabled on the sobject for whatever you are passing in your WhatId.
Credit goes to the users in the following link. I just copied their solution for you.
http://forums.sforce.com/t5/Apex-Code-Development/SingleEmailMessage-using-setTemplateId-NEED-URGENT-HELP/td-p/98813/page/2
I made sure that saveAsActivity = false and that I have Allow Activities checked off for the object and both are done but I am still getting the same exception. It is weird because that is the exact problem I am having (In the link you posted) but the resolution doesn't work...
So I got around the other exception by simply not specifying a User ID in the setTargetObjectId() method. Now I am getting another error which I have never seen before and cannot find any documentation on. This time I will post the relevant code snippet and the error. First the code. This code just sends an email when a new Feature_Setup__c object is created. I am specifying the Id of the Feature_Setup__c object in the setWhatId() method. This shouldn't be a problem because the documentation says a custom object can be specified. Here is the code:
And this is the exception error I get when my tests run:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, New_Feature_Setup_Send_Email: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Opportunity/Account ID: id value of incorrect type: a0L30000004Wci6EAC: [WhatId] Trigger.New_Feature_Setup_Send_Email: line 89, column 17
Honestly I am out of ideas at this point... Any help would be greatly appreciated. Thanks in advance.
Hi DaveHagman,
I am not sure but please try once after ensuring that the profile from which you are running these classes/trigger must have "Visible" rights for all fileds of Task/Activity/Event (Field Level Security).
You can do this by :
Setup > Administration Setup > Manage Users > Profiles > System Administrator > Field-Level Security > View (Task)
Give "Visible" rights to all fields and try.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi Ankit,
Thanks for the response. I checked the Field Level Security and everything is set to Visible. I did finally solve this issue but the weird thing is I didn't do anything differently. I think there was some kind of system issue with SF because all of a sudden the code worked without throwing an exception. Very weird but everything is working fine now.