WebService static String send_InvoicePdf(String invid){
        if(String.isNotBlank(invid) ){
            String query = 'SELECT ';
            for(String s : fields(Invoice__c.getSobjectType())){
                query += s+', ';
            query += ' Contact__r.Name';
            query = query.removeEnd(', ');
            query += ' FROM Invoice__c WHERE Id =:invid';
            Invoice__c record = Database.query(query);
            EmailTemplate salesInvoice = [SELECT Id, Subject, body, HtmlValue FROM EmailTemplate WHERE Name = 'Sales Invoice Email'];
            EmailTemplate finalInvoice = [SELECT Id, HtmlValue,body,  Subject FROM EmailTemplate WHERE Name = 'Final Invoice Email'];
            EmailTemplate proformaInvoice = [SELECT Id, HtmlValue, body, Subject FROM EmailTemplate WHERE Name = 'Proforma Invoice Email'];
            /*record.Email_to_MD__c = true;
                Update record;
            System.debug('*************record *****************'+record );

            PageReference pdf = new PageReference('/apex/InvoicePDF?scontrolCaching=1&id='+record.Id);
            system.debug('id is'+pdf);
            // the contents of the attachment from the pdf
            Blob body;

            try {
                //returns the output of the page as a PDF
                body = pdf.getContentAsPdf();
                //need to pass unit test -- current bug  
            }catch (Exception e) {
                body = Blob.valueOf('Some Text');
            //Added by divya 2 Jan,2018
            String templateId;
           /* Decimal d = record.Remaining_Amount__c;
            String sRemaining;
            if (!string.valueof(d.format()).right(3).contains('.')){
                sRemaining = string.valueof(d.format()) + '.00';
            }else if (string.valueof(d.format()).right(2).contains('.')){
                sRemaining = string.valueof(d.format()) + '0';
            }else {
                sRemaining = string.valueof(d.format());
            String subject;
            String attchName;
            If(record.Invoice_Type__c=='Proforma Invoice'){
                subject = renderTemplate(record, proformaInvoice.Subject);
                templateId = proformaInvoice.Id;
                attchName = record.Invoice_Number_Text__c + ' Proforma Invoice.pdf';
            else if(record.Invoice_Type__c=='Sales Invoice'){
                subject = renderTemplate(record, salesInvoice.Subject);
                templateId = salesInvoice.Id;
                attchName = record.Invoice_Number_Text__c + ' Sales Invoice.pdf';
            else if(record.Invoice_Type__c=='Final Invoice'){
                subject = renderTemplate(record, finalInvoice.Subject);
                templateId = finalInvoice.Id;
                attchName = record.Invoice_Number_Text__c + ' Final Invoice.pdf';

            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.Body = body;

            Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(templateId, null, record.Id);
            String toaddress = record.Email__c;
            String[] toAddresses = new String[] {toaddress};
            mail.setToAddresses( toAddresses );
           // mail.setHtmlBody(testMail.getHtmlBody());

            if( String.isNotBlank( record.Additional_Email__c ) ){
                String[] CCAddress = new String[]{};
                CCAddress = record.Additional_Email__c.split(';');
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

            Attachment att = new Attachment(
                ContentType = 'application/pdf',
                Name = 'Invoice.pdf',
                Body = body,
                parentId = invid
        return null;

    catch(Exception e){
        return null;

this method is called upon button click, it works fine for non partner community user

Error : Unable to retrieve object Line : body = pdf.getContentAsPdf();

Here is the button code


var em ='{!Invoice__c.Email__c }' ;

var txt;
var r = confirm("Do you want to continue sending Invoice to the following email -"+'{!Invoice__c.Email__c }');
if (r == true) {
if(em!='' || em!=null){
var error = document.getElementById('errorDiv_ep');
var ParentName = sforce.apex.execute("Email_Pdf","send_InvoicePdf", {'invid':'{!Invoice__c.Id}'});
var n = ParentName.indexOf("http");
txt = "Email sent..";

else {
txt = "Email Id not found..";
location.reload ();
  • Make sure the Page is enabled for the Community User Profile
    – Ideasforce
    Feb 20, 2020 at 12:12
  • @Ideasforce It is already enabled for community profile and also have access to all objects Feb 20, 2020 at 12:17

2 Answers 2


Here is a question for the same problem (with a proposed solution).

From the aforementioned post:

When you pass the ID to the PDF controller directly after the insert from the same controller method the record is not available. You can output the ID in the PDF and see that it is clearly getting passed through. You can select the record back in the method where you did the insert, but for some reason the record is not available in the PDF controller. I suspect there is something going on behind the scenes with how the transactions and/or transaction contexts are handled.

In your case the update line may be the problem, and my guess is you can work it around with the same proposed solution. i.e.: After the update, forward to a blank page that gets the pdf and sends the email.

  • Also note you are updating the Invoice__c record with no changes, you may want to include the commented "record.Email_to_MD__c = true;" line within the if statement that follows
    – Ideasforce
    Feb 20, 2020 at 13:07
  • Hi , if you see closely the whole code for update is commented already so update is not the issue Feb 20, 2020 at 13:15
  • Ah! Sorry I didn't notice it was all commented. I guess the "record" result of the dynamic query is also not resolved when you call the getContentAsPDF. Try changing "record.Id" for "invid" in the URL for the new PageReference
    – Ideasforce
    Feb 20, 2020 at 15:17
  • I already tried with "invid", but it results the same, and I am sure "record.id" is resolved as I saw debug logs of VF page's controller Feb 25, 2020 at 5:20

I solved the problem by using vf page to invoke apex from button instead of calling apex method from execute javascript (Webservice method).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .