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
GYAN ANDRUSGYAN ANDRUS 

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

APatAPat
If you want to call your apex class from javascript using custom button, then you will have to define your class as global and class methods as a webservice .

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!
Dushyant SonwarDushyant Sonwar
Hi Hema,
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",{});

 
GYAN ANDRUSGYAN ANDRUS
Thanks for the reply,

        Still that error showing like unexpected token illegal
Dushyant SonwarDushyant Sonwar
I think you have extra character in your script as i have tested your code and it is working fine
See this link
http://stackoverflow.com/questions/12719859/syntaxerror-unexpected-token-illegal
this will help you.
GYAN ANDRUSGYAN ANDRUS
thank you so much,But the error is not rectified Dushyant Sonwar,The error showing in class that unable to retreive object in  line 38,I want to send Email attachment for discount Approval form(custom object),but the class is For Account,my vf page is Discount Approval formPDF,
 
GYAN ANDRUSGYAN ANDRUS
I have Changed the Class for CUSTOM OBJECT:
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;
    }
}