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
Phuc Nguyen 18Phuc Nguyen 18 

Email existing Visualforce PDF

Hello All,
I have a VF page that renders record information as shows it as a PDF .  I am using renderAs="PDF"
How can I now email that PDF?
I thought I needed an email controller show below
RequestPDF is the name of the VF PDF page that is rendering the data.  How do I pass the id to the controller?  I would like to try with either a button or maybe when a user updates and saves the record.  Either way the id and email address is sent to the controller.  And if the email address is null prompt for 1? 
public with sharing class RequestPDF_EmailController {
    
    @RemoteAction
    public static string SendAttachment(String sEmailAddress, String tempId){
        String sMessage='';
        try{            
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

            PageReference pref = page.RequestPDF;
            pref.getParameters().put('id',tempId);
            pref.setRedirect(true);
            Blob b = pref.getContent();
            attach.setFileName('AttachedInV.pdf');
            attach.setBody(b);
            semail.setSubject('Inv Details');
            semail.setToAddresses(new List<String>{sEmailAddress});
            semail.setPlainTextBody('Please find the attached INV details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            sMessage='SUCCESS';
        }
        catch(Exception ex){
            sMessage=ex.getMessage()+'\n'+ex.getLineNumber()+'\n'+ex.getCause();
        }
        return sMessage;
    }
 }

Thank you,
P
Best Answer chosen by Phuc Nguyen 18
ANUTEJANUTEJ (Salesforce Developers) 
>> https://salesforce.stackexchange.com/questions/205/call-a-remoteaction-from-a-custom-button

As mentioned in the above link "Visualforce includes a bunch of javascript stub methods and helpers based on the controllers used that have @RemoteAction methods in them. Custom buttons lack the intelligence to do this and I believe @RemoteAction calls are not possible in this way from buttons.

That at least would be the official answer, if you were willing to reverse engineer how @RemoteAction methods actually pass data back to the server it may be possible to make an unsupported implementation."

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Phuc,

>> https://developer.salesforce.com/blogs/developer-relations/2008/06/emailing-visual.html

There is an implementation in the above link that has a description of the implementation of how it can be done can you try checking it out.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Phuc Nguyen 18Phuc Nguyen 18
Thank you for the reply Anutej. 
I think I have all this I just need to be able to send it.  Can I initiate the send via a button or based on a field update?
Thanks
P
ANUTEJANUTEJ (Salesforce Developers) 
>> https://salesforce.stackexchange.com/questions/205/call-a-remoteaction-from-a-custom-button

As mentioned in the above link "Visualforce includes a bunch of javascript stub methods and helpers based on the controllers used that have @RemoteAction methods in them. Custom buttons lack the intelligence to do this and I believe @RemoteAction calls are not possible in this way from buttons.

That at least would be the official answer, if you were willing to reverse engineer how @RemoteAction methods actually pass data back to the server it may be possible to make an unsupported implementation."
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hello Anutej, I removed the remote action and tried going tith an aura component but its not doing anything and not sure why. If you have an idea for a best approach I would love to hear it.  Did not think this would be that difficult.  I have a record where i want to grab values from teh parent and child record.  Save it as a pdf and send. I thought a VF would be easiest but VF page cannot be refernced on object and JS for the button is not best and not supported in lightning.  Let me know what your approach would be for this use case.  Thanks again.
P