Thursday, October 3, 2013

The Create PDF button on Quote

I was asked about the ability to restrict the creation of a PDF for a given quote until a couple business rules had been met.  As I thought about the solution, a couple ideas came to mind:

1. Add a record type for the ok Quotes and assign that record type a new layout that includes the Create PDF button.  Remove the Create PDF from the other page layout.
2. Modify the behavior of the existing Create PDF button to check the status first.
3. Create a new VF page to replicate the Create PDF functionality.

Given the timing and other project considerations, I opted for #2, if it was feasible.  I knew that #1 would work but I was worried about introducing a record type and then having to roll it back when another quote-related project went live.

To start, I had to figure out if I could see the code that the out-of-box button was calling to render the PDF.  With chrome it was pretty easy to inspect the element and see the code the button was calling.

With a minor bit of additional javascript, the quote's status can be interrogated first and an informational alert raised, if certain business criteria are met:

/*********************************************************************
if('{!Quote.Status}'!= 'XYZ' ) 
{
// do some business logic...
var isOk = true;  
} if(isOk) 

var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay']; 

pdfOverlay.dialog.buttonContents = "<input value=\'Save to Quote\' class=\'btn\' name=\'save\' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'0\',\'0\');\" title=\'Save to Quote\' type=\'button\' ><input value='Save and Email Quote' class='btn' name='saveAndEmail' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'1\');\"; title='Save and Email Quote' type='button' ><input value=\'Cancel\' class=\'btn\' name=\'cancel\' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\" title=\'Cancel\' type=\'button\' >"; 

//change this to use the correct template for your business/environment!! 
pdfOverlay.summlid = 'XXXXXXXXXXXXX'; 

pdfOverlay.setSavable(true); 

//change this to use the quote id 
pdfOverlay.setContents('/quote/quoteTemplateDataViewer.apexp?id={!Quote.Id}','quote/quoteTemplateHeaderData.apexp?id={!Quote.Id}'); 

pdfOverlay.display(); 

else 

//raise an alert to let the user know about some business rule
alert('The Quote requires XYZ before the PDF can be generated.'); 
}

*************************************************************/




8 comments:

  1. Hi, where in Salesforce did you change/add this javascript?

    New to this development game but this is a requirement for us also :)

    Thanks,
    Tom

    ReplyDelete
  2. Not to worry, I've created a new cutom button... And it works!

    Now i just need to work out how to make it select a template based on pre selected options on the quote page...

    Thanks,
    Tom

    ReplyDelete
  3. Managed to do it using a custom field in place of the id - The custom field being a formula that is an if statement giving certain ids depending on which criteria are met.

    Amazing.

    Thank you!

    ReplyDelete
  4. hi Tom, sorry been away from the blog for a few days. Thanks for sharing your results. Awesome!
    Mike

    ReplyDelete
  5. Hi Michael,

    how can i pass the Quote.Id on the script? Can you give an example? What should I put here? (id={!Quote.Id}')

    Thanks
    Dimi

    //change this to use the quote id
    pdfOverlay.setContents('/quote/quoteTemplateDataViewer.apexp?id={!Quote.Id}','quote/quoteTemplateHeaderData.apexp?id={!Quote.Id}');

    ReplyDelete
    Replies
    1. Hi Dimi, sorry for the delay in my reply. I trust you have figured it out by now. For others - the {!Quote.Id} is how Salesforce does the substitution. Leave it as-is and it should work for the quote you are on.

      Delete
  6. I have implemented the same process for my org, it's been working great until recently. Wondering if you have any suggestions.....
    When the overlay of the PDF is rendered and the user clicks the 'Save to Quote' button, nothing happens. The other 2 buttons are working as expected. I added a try catch block to see if any exceptions would alert but none did.

    ReplyDelete
  7. I have implemented the same process for my org, it's been working great until recently. Wondering if you have any suggestions.....
    When the overlay of the PDF is rendered and the user clicks the 'Save to Quote' button, nothing happens. The other 2 buttons are working as expected. I added a try catch block to see if any exceptions would alert but none did.

    ReplyDelete