You need to sign in to do that
Don't have an account?
Robert Berkeley
Trying to send an email from Account page
Hi, I'm trying to send an email when a client's Account is 'opened'. I have a button on the Account page with a Javascript call beneath it:
In the APEX class being called above I then try to load a Visual Force email template to merge fields from the Account object into its HTML for the email.
The system was telling me that I could not use an Account.Id for the setTargetObjectId call, so I got the idea to try to get the Contact.Id from the Account record. Am I on the right tracks?
I now get the error:
sforce.apex.execute("AccountOpenEmail","SendEmail", {id:"{!Account.Id}"});
In the APEX class being called above I then try to load a Visual Force email template to merge fields from the Account object into its HTML for the email.
global class AccountOpenEmail { WebService static void SendEmail(string id) { Account acc = Database.query('SELECT Account.Name, (SELECT Contact.Id FROM Account.Contacts) FROM Account WHERE id = ' + id); Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setTargetObjectId(acc.Contacts[0].Id); email.setTemplateId('00X11000000DkqK'); email.setBccSender(true); Messaging.SendEmailResult [] r = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email}); for ( Messaging.sendEmailResult result : r ) { if ( !r[0].isSuccess () ) { System.debug ( result ); } else{ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' ); ApexPages.addMessage(msg); } } } }
The system was telling me that I could not use an Account.Id for the setTargetObjectId call, so I got the idea to try to get the Contact.Id from the Account record. Am I on the right tracks?
I now get the error:
{faultcode:'soapenv:Client', faultstring:'System.QueryException: unexpected token: 'IfLgJ' Class.AccountOpenEmail.SendEmail: line 4, column 1', }
Seems like the Account object is not getting any relatedTo data, but you can get that data through the 'recipient' data object instead. My Visualforce email template now looks like this:
It is now merging the correct data into the email template!
All Answers
try below code...
Thanks,
Rockzz
I tried the APEX code you posted above but could not get anything from the first SOQL select. The UserInfo.getUserId() is returning a NULL and so there's nothing in the Usr record. I am getting record data from the 2nd SOQL select though, so using the Contact.Id from there I am able to instantiate the email template.
My problem now is that while it does now send and email (yay!) it does not merge data from the Account object into that email. Here is the APEX code I am now using:
And here is the Visual Force email template I am using:
I tried using the annotation {!Account.Name} but it complained that:
I read somewhere else in the docs that I should rather be using {!relatedTo.Name} so I am using that instead.
Is this how you merge Account data into an email template?
Seems like the Account object is not getting any relatedTo data, but you can get that data through the 'recipient' data object instead. My Visualforce email template now looks like this:
It is now merging the correct data into the email template!
If this solves your problem, kindly mark it as the best answer.
Thanks,
Rockzz