Assuming you have this button on a related list/list view, you can use the {!GETRECORDIDS} javascript function to get the list/array of id of the selected records. You can then send this array to your apex class and then query the email field to get the email. YOu can do something like this:
{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}
var recordIdArray = {!GETRECORDIDS($ObjectType.Contact)};
console.log('the record Ids are' + recordIdArray);
var retStr = sforce.apex.execute("outbound_Emails", "SendEmailNotification",{conId: recordIdArray});
global class outbound_Emails{
webservice static void SendEmailNotification (list<id> conId){
list<contact> conList = [SELECT name,email FROM contact WHERE id IN : conId];
system.debug('the selected contacts are ' + conList);
//you can iterate over this list to get the email and send the mail
}
}
The email author URL should look like below. Out of all the parameters
I would always use "p3_lkid" and "retURL". Rest of the parameters are
optional. Let's break this down:
_ui/core/email/author/EmailAuthor?p3_lkid=001E000000CVCF7&retURL=%2F001E000000CVCF7&p2_lkid=003180000066S1e&p4=abc@cc.com&p5=abc@bcc.com;xyz@bcc.com&p6=Hello World&p23=Nice to meet you&p24=asd@additional.com&template_id=
p3_lkid = "Related To" Id. Based on the Id, Salesforce will
automatically select the object and record. This Id will be used to
populate the merge field, if you are also using a template.
retURL = Return URL if cancel is pressed. Followed by %2F.
p2_lkid = Contact Id. Use to populate "To" field. It automatically
fetches the email of this contact to send email. Also, populates the contact merge fields, if used.
p4 = CC
p5 = BCC. I passed two emails separated by semicolons to keep two
different people in the loop. Same goes with CC field.
p6 = Subject of this email.
p23 = Email body.
p24 = Additional emails.
template_id = Id of template you are going to use. This template
should contain merge fields of the same object as p3_lkid. No need to
pass p6 and p23, if the template already handles them.
Now, coming to your case. There is no need to keep the behavior as javascript. URL behavior could have also met your requirement. Hence, I am modifying your code considering the behavior as URL.
/_ui/core/email/author/EmailAuthor?retURL=%2F{!Opportunity.Id}&p24=varunreddypenna@gmail.com;v.reddy@thegordiangroup.com&template_id=00Xj0000000J8sI&p3_lkid={!Opportunity.Id}&p5=penna.janareddy@gmail.com
Removed P3 - If you're using p3_lkid, then no use to do this.
Removed p26 - NOT sure what you trying to achieve here.
Used semi-colons between emails.
NO need to use location.replace and inverted commas. Simply copy paste the url with behavior as "URL".
Also, you should use merge field for "p24" by creating formula field for that.
To add more parameters, please see the structure above.
PS:- It would better if you query the template based on its developer name. However, then you will have to execute the button as javascript.
Best Answer
I ran this code and result is not quite what you think.
If you
alert
the whole result object, you get this:Your code needs to look like this to actually work:
There were several other errors too - the key is to output the whole error in an alert so you can debug it.
As you can see, the
targetObjectId
is the user that the email is going to, not the object that the template refers to.Also,
saveAsActivity
needs to be false to send to an internal user.This is probably going to take quite a bit of work to get going, but hopefully you are now on the right track.