+ Start a Discussion
sfdcFanBoysfdcFanBoy 

how to call standard EmailQuote() method in visualforce page?

I wanna call the standard 'EmailQuote()' method of quotes in my custom visualforce page. 

 

VF Page:

<apex:page standardController="quote" extensions="quoteSample">

<apex:form >
<apex:pageBlock >
    <apex:outputfield value="{!quote.Name}"/>
    <apex:commandButton action="{!EmailQuote}" value="Email"/>
    
    </apex:pageBlock>
</apex:form>

</apex:page>

 Class:

 

 

public with sharing class quoteSample {    
public quote q;        

public quoteSample(ApexPages.StandardController controller) {
    this.q = (quote)controller.getRecord();    }  

public pagereference EmailQuote(){
        EmailQuote();        
        PageReference newPage = New PageReference('get('id'));
newPage.setRedirect(true); return newPage; } }

 

Error:

 

Maximum stack depth reached: 1001

Error is in expression '{!EmailQuote}' in component <apex:page> in page quotesample

 

 

An unexpected error has occurred. Your development organization has been notified.

 

Best Answer chosen by Admin (Salesforce Developers) 
Michael_TorchedloMichael_Torchedlo

You ask about using the standard controller, but you are already using a custom controller extension in the sample you posted. 

 

   <apex:page standardController="quote" extensions="quoteSample">

 

I don't know of any way to use a "standard" Create PDF or send Email method.  Actually, I don't think those methods exist for us to use, I think that functionality on the standard page is handled with scripts or other URL redirects.  However, in your controller extension you can create a method that returns a PageReference to the standard Email page.  When you create the URL to the new pagereference, you can pass in the appropriate URL parameters:  the id of your quote, contact, and quote document (quote pdf).  This is an example of how to create the URL

 

public PageReference EmailQuote(){

   PageReference newp = new PageReference('/_ui/core/email/author/EmailAuthor?p2_lkid='+Contact.id+'&rtype=003&p3_lkid='+Quote.id+'&doc_id='+QuoteDocument.id+'&retURL=/'+Quote.id);

 

   return newp;

}

 

This will only work the way you want it to if you pass in all the IDs.  Otherwise, it will not be able to automatically add the PDF as an attachment, and it will not auto-populate the Contact name in the To: field.  So you have to make sure that you also program your controller to have those IDs available.

 

example Email page url (no spaces):

hxxps://na13.salesforce.com/_ui/core/email/author/EmailAuthor?p2_lkid=003a00000XXXXXX&rtype=003&p3_lkid=0Q0a000000XXXX &doc_id=0QDa0000000XXXX&retURL=/0Q0a000000XXXX

 

 

You can also make your own method to Save PDF as attachment.  I assume you are using a custom VF page which renders as a PDF, and that is the output file you want to save.  Sample code below.

 

 

public PageReference SavePDF(){
   PageReference pdfPage = Page.CustomPDFPageName;
   Blob pdfBlob = pdfPage.getContentasPDF();
   QuoteDocument a = new QuoteDocument(quoteid = MQuote.id, document = pdfBlob);
   Database.SaveResult MySaveResult = Database.Insert(a, false);
   PageReference newp = new PageReference('/'+Quote.id);
   pdfBlob = null;    //a=null;
   return newp;

}

 

Of course, if you want to combine both functions (create the PDF attachment and then redirect to the Email page) then you can modify this code as you see fit.

 

I hope this helps.  I did implement a similar solution for our org, so I know it is possible to do this without too many lines of code, but I don't know what the rest of your controller looks like or what other work you want it to perform before you save.

All Answers

bob_buzzardbob_buzzard

There isn't a standard emailquote as far as I'm aware.  This code:

 

public pagereference EmailQuote(){
        EmailQuote();    

 simply introduces recursion - the first thing the EmailQuote method does is call the EmailQuote method.  

sfdcFanBoysfdcFanBoy
My bad! Actually, I am trying to replicate the standard 'Create PDF' functionlaity available in Quotes as we are hitting limits on the number of fields that can be included in the PDF (quote template). The Create PDF functionality provides a PDF preview of the chosen fields and also gives couple of buttons to - 'Send Email with this PDF as attachment (i.e, EmailQuote)' and 'Save PDF as attachment in Quotes'. Is there any way to call these standard methods directly using standard controller instead of going for custom coding these functionalities?
Michael_TorchedloMichael_Torchedlo

You ask about using the standard controller, but you are already using a custom controller extension in the sample you posted. 

 

   <apex:page standardController="quote" extensions="quoteSample">

 

I don't know of any way to use a "standard" Create PDF or send Email method.  Actually, I don't think those methods exist for us to use, I think that functionality on the standard page is handled with scripts or other URL redirects.  However, in your controller extension you can create a method that returns a PageReference to the standard Email page.  When you create the URL to the new pagereference, you can pass in the appropriate URL parameters:  the id of your quote, contact, and quote document (quote pdf).  This is an example of how to create the URL

 

public PageReference EmailQuote(){

   PageReference newp = new PageReference('/_ui/core/email/author/EmailAuthor?p2_lkid='+Contact.id+'&rtype=003&p3_lkid='+Quote.id+'&doc_id='+QuoteDocument.id+'&retURL=/'+Quote.id);

 

   return newp;

}

 

This will only work the way you want it to if you pass in all the IDs.  Otherwise, it will not be able to automatically add the PDF as an attachment, and it will not auto-populate the Contact name in the To: field.  So you have to make sure that you also program your controller to have those IDs available.

 

example Email page url (no spaces):

hxxps://na13.salesforce.com/_ui/core/email/author/EmailAuthor?p2_lkid=003a00000XXXXXX&rtype=003&p3_lkid=0Q0a000000XXXX &doc_id=0QDa0000000XXXX&retURL=/0Q0a000000XXXX

 

 

You can also make your own method to Save PDF as attachment.  I assume you are using a custom VF page which renders as a PDF, and that is the output file you want to save.  Sample code below.

 

 

public PageReference SavePDF(){
   PageReference pdfPage = Page.CustomPDFPageName;
   Blob pdfBlob = pdfPage.getContentasPDF();
   QuoteDocument a = new QuoteDocument(quoteid = MQuote.id, document = pdfBlob);
   Database.SaveResult MySaveResult = Database.Insert(a, false);
   PageReference newp = new PageReference('/'+Quote.id);
   pdfBlob = null;    //a=null;
   return newp;

}

 

Of course, if you want to combine both functions (create the PDF attachment and then redirect to the Email page) then you can modify this code as you see fit.

 

I hope this helps.  I did implement a similar solution for our org, so I know it is possible to do this without too many lines of code, but I don't know what the rest of your controller looks like or what other work you want it to perform before you save.

This was selected as the best answer
sfdcFanBoysfdcFanBoy

That's the best reply ever, mate!

 

The documentId part was what I was looking for. Email PDF as attachment.  Thanks.

 

 

Cheers!

 

llisallisa
Hello Everyone,

I wrote my function currectly .Functionality was to save the pdf and email it with the respective attachment.
Here it saved but not attached in my email page .

public PageReference attachPdfandEmail() 
    {
        PageReference pdf;
        pdf= Page.Quote_PDF;
        pdf.getParameters().put('id', selectedQuote);
        Blob pdfBlob = Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : pdf.getContent();
        Attachment myAttach = new Attachment();
        myAttach.Parentid = quotes.id;
        myAttach.name = 'Quote_v'+(totalRecords_Attachment+1)+'.pdf';
        myAttach.body = pdf.getContentAsPdf();
        insert myAttach;
        system.debug(myattach.id);
        PageReference ref=new PageReference('/_ui/core/email/author/EmailAuthor?p2_lkid='+acc.id+'&rtype=003&p3_lkid='+quotes.id+'&doc_id='+myattach.id+'&retURL='+quotes.id);
        ref.setRedirect(true);
        return ref;
    }

But while returning the valur the URL changes by itself like :
https://ap2.salesforce.com/_ui/core/email/author/EmailAuthor?doc_id=00P28000006CB6ZEAW&p2_lkid=0012800000EHkmRAAT&p3_lkid=a092800000DdH7ZAAV&retURL=a092800000DdH7ZAAV&rtype=003
Doc id comes firct by itselt and rtype=003 comes last.

please help on this.