You need to sign in to do that
Don't have an account?
Hi I need to sent an email attachment for clicking on submit custom button,i have written the code for email attachment,and i have written the onlick javascript for custom button
APEX CLASS:
public class sendEmailobject {
public sendEmailobject(ApexPages.StandardController controller) {
}
public String subject { get; set; }
public String body { get; set; }
private final Account account;
// Create a constructor that populates the Account object
public sendEmailobject () {
}
public Account getAccount() {
return account;
}
public PageReference send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the account ID
PageReference pdf = Page.attachmentPDF;
pdf.getParameters().put('id','0019000000GFM34');
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContent();
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.pdf');
efa.setBody(b);
list<string> toAddresses = new list<string>();
toAddresses.add('mpriyajose10@gmail.com');
// Sets the paramaters of the email
email.setSubject( 'Hi');
email.setToAddresses( toAddresses );
email.setPlainTextBody( 'Hi');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
BUTTON ONCLICK JS:
{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}
var retStr;
retStr = sforce.apex.execute("sendEmailobject","send{});
alert('The method returned: ' + retStr);
document.location = '/{!Account.Id}';
But it showing error ..please help me
Other way to do is create a VF page with controller. You java script will invoke VF page and in the action paramter execute send method..
Hope this helps!
I Made some changes In your Class and script
Global class sendEmailobject {
public sendEmailobject(ApexPages.StandardController controller) {
}
public String subject {
get;
set;
}
public String body {
get;
set;
}
private final Account account;
// Create a constructor that populates the Account object
public sendEmailobject() {
}
public Account getAccount() {
return account;
}
webService static string send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Create the email attachment
/ Reference the attachment page and pass in the account ID
PageReference pdf = Page.attachmentPDF;
pdf.getParameters().put('id','0019000000GFM34');
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContent();
list < string > toAddresses = new list < string > ();
toAddresses.add('mpriyajose10@gmail.com');
// Sets the paramaters of the email
email.setSubject('Test Subject');
email.setToAddresses(toAddresses);
email.setPlainTextBody('Hi');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult[] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
email
});
return null;
}
}
and
Script
{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}
var retStr;
var retStr = sforce.apex.execute("sendEmailobject" ,"send",{});
alert('The method returned: ' + retStr);
document.location = '/{!Account.Id}';
===============
If your Org Has a NameSpace then just change the one line
var retStr = sforce.apex.execute("sendEmailobject" ,"send",{});
to
var retStr = sforce.apex.execute("Namespace of your org.sendEmailobject" ,"send",{});
Still that error showing like unexpected token illegal
See this link
http://stackoverflow.com/questions/12719859/syntaxerror-unexpected-token-illegal
this will help you.
Global class sendEmail1 {
public sendEmail1(ApexPages.StandardController controller) {
}
public String subject {
get;
set;
}
public String body {
get;
set;
}
private final Discount_Approval_Request__c discount;
// Create a constructor that populates the Account object
public sendEmail1() {
}
public Discount_Approval_Request__c getAccount() {
return discount;
}
webService static string send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Create the email attachment
// Reference the attachment page and pass in the account ID
PageReference pdf = Page.DiscountApprovalForm_PDF;
pdf.getParameters().put('id','a08N0000005tUEx');
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContent();
list < string > toAddresses = new list < string > ();
toAddresses.add('mpriyajose10@gmail.com');
// Sets the paramaters of the email
email.setSubject('Test Subject');
email.setToAddresses(toAddresses);
email.setPlainTextBody('Hi');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {});
// Sends the email
Messaging.SendEmailResult[] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
email
});
return null;
}
}