You need to sign in to do that
Don't have an account?
Carter85
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.
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.
All Answers