function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carter85Carter85 

Help with send email method

I have a method intended to generate an email with a users login credentials with a button click.  I used to have it pulling the information off a custom object but am now trying to consolidate with just the standard Contact object, but now in my tests it throws an error in execution.  My code is:
public with sharing class CM_InfoController{

        private static String ORG_WIDE_EMAIL_ADDRESS = 'customerservice@maximusautogroup.com';
               
        public Contact user    {get;set;}
        private static ID orgWideEmailId = [SELECT id FROM OrgWideEmailAddress WHERE address=:ORG_WIDE_EMAIL_ADDRESS].id;
               
    public CM_InfoController(){
    }
   
    public CM_InfoController(ApexPages.StandardController controller){
       user = (Contact)controller.getRecord();
    }   
   
    public PageReference sendEmail(){
        PageReference pageReference = null;
        if(user != null){
                Contact nu = [SELECT Id, Name, Password__c, Email FROM Contact WHERE id=:user.id];
                if(orgWideEmailId != null){
                  // Build new URL
                  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                  String htmlBody = '<h1>MAG User Info Request</h1>' +
                  '<p>Dear ' + nu.Name + ',</p>' +
                  '<p>We have received your request for user credential information in order to access some restricted features of the Maximus Auto Group website.</p>' +
                  '<p>Your Username is: ' + nu.Email + '</p>' +
                  '<p>Your Password is: ' + nu.Password__c + '</p>' +
                  '<p>If you have any questions or concerns regarding this information, please contact one of our associates at 1-800-801-1342 and we will assist you, or reply to this email with any specific questions.</p>' +
                  '<p>Thank you for your business and continued support!</p>' +
                  '<p>MAXIMUS AUTO GROUP</p>';
                                                                      
                   //Build Email
                   mail.setOrgWideEmailAddressId(orgWideEmailId);             
                            String[] toAddresses = new String[]{nu.Email};                                              
                                            mail.setToAddresses(toAddresses);
                                            mail.setSubject('MAG User Access');
                                            mail.setHtmlBody(htmlBody);
                                            mail.setWhatId(user.id);
                                       
                                        Messaging.sendEmail(new Messaging.Email[]{mail},true);
                                                                                                           
                            ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.INFO, 'Message sent successfully');                  
                            ApexPages.addMessage(msg);                                                                                                 
                                        }
                                        else{
                            ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.ERROR, 'Org wide email address not found ['+ ORG_WIDE_EMAIL_ADDRESS + ']');                  
                            ApexPages.addMessage(msg);                                                             
                                        }                                                                                                     
                            }   
                                else{
                                ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.ERROR, 'Email not specified on user to send information.');                  
                                ApexPages.addMessage(msg);                                                             
                                }
                                return(pageReference);
                                }
                                }

And the error I encounter each time is: "First exception on row 0; first error: INVALID_ID_FIELD, Only accounts, assets, campaigns, cases, contracts, opportunities, orders, products, solutions and custom objects are allowed as whatId.: "  which is directed at the "mail.setWhatId(user.id);" line.  However, this was working properly with the custom object I used to have in place so I am unsure why this would suddenly not work when all I did to the code was change the object referenses from my custom one to the Contact one.
Best Answer chosen by Carter85
logontokartiklogontokartik
You dont need to setWhatId in your email as you are not using any merge fields. Try removing/commenting the mail.setWhatId(user.id).

All Answers

logontokartiklogontokartik
You dont need to setWhatId in your email as you are not using any merge fields. Try removing/commenting the mail.setWhatId(user.id).
This was selected as the best answer
Carter85Carter85
That did the trick, thanks.