+ Start a Discussion
Andy BoettcherAndy Boettcher 

Overriding Quote CREATE PDF dropdown to inject business logic to choose Quote Template

Good evening!

 

Here is my business scenario - let me know if I can provide any more information or if you have another idea for me...

 

  1. Quote is created
  2. User enters Quote information (Line Items, etc)
  3. User presses "Create PDF"
  4. System checks certain business rules (via APEX)
  5. If rule A is in effect, Quote Tempate A is chosen and a PDF is generated / attached to the Quote
  6. If rule B is in effect, Quote Template B is chosen and a PDF is generated / attached to the Quote

Essentially, the situation is that if "A" is in effect, the "Draft" watermarked Quote Template is the ONLY template that can be used.  Conversely, if "B" is in effect, the "Final" un-watermarked Quote Template is the ONLY template that can be used.

 

To the user, they aren't allowed the choice of which template to use.  The system just picks it for them based on the appropriately parsed business logic fork.

 

I have been able to successfully figure out how to get the standard CreatePDF JS-driven button on a new VF page.  The correct template appears, the correct data is merged, however when I enter the "savePDF" JS function and it submits to SF, I get a generic failure message back with no obvious next step:

 

while(1);
{"quotePDFSaveError":"Error saving PDF to quote.  Please try again."}

 

The above error message comes back after submitting a long GET (or POST, tried them both) string to <instance>/_ui/sales/quote/document/QuotePDFServlet?<bunch of variables parsed by JS>

 

So in lies my question to the masses - has anyone done this?  Does anyone have an alternate approach / solution to my customer need?  Does anyone know what this cryptic error message means?

 

-Andy

SoleesSolees

Create this webservice:

global class AttachmentGenerator
{
   webService static String AttachPDFToQuote(string Id)
   {
        string retRes = '';
        try
        {
         PageReference pageRef = new PageReference('/apex/Prueba_PDF?Id='+Id); <--- Your apex page
         Blob content = pageRef.getContent();
         QuoteDocument doc = new QuoteDocument(Document = content, QuoteId = Id);
         insert doc;

         retRes='SUCCESS';
         return retRes;
       }catch(exception ex)
       {
           System.debug('--- Error ----------'+ ex);
           retRes= ex.getMessage();
           return retRes;
       }
   }
}

 

And then use this JS function i created:

function SaveQuotePDF(a) {
    try {
        var res = sforce.apex.execute("AttachmentGenerator","AttachPDFToQuote",{Id : a});
        if(res=='SUCCESS')
            window.location.reload();
        else
            alert('Error in attaching file ----'+ res);
    } catch(er) {
        alert(er);
    }
}