You need to sign in to do that
Don't have an account?

Need help with using apex to select email template and attach PDF
I have extended the standard Contract object so it now has a master detail relationship with an object called Contract_Line_Item__c which calculates maint and displays what is maint is being renewed on.
I also have a VisualForce page that renders a great PDF now that is properly formatted to display the needed data.
THE PROBLEM:
I need your help to figure out the best way via apex to have this PDF attach itself to....
1. The contract itself,
2. An HTML email template and send it automatically to the customer/contact listed on the contract,
3. Lastly, the ability to have the PDF print automatically for snail mail purposes....
I will give kudos to anyone that contributes.
Thank you,
Steve Laycock
Yes, you'd create a new Visualforce page and just attach that to a button on the contract. As for emailing the attachment, you'll just need some URL hacking. I don't have the time to do the research on this, but try searching the forums. I'm sure someone has done something like that before.
All Answers
I've written all three of these items (our product also has contracts/contract line items...). These three tasks are a lot easier if you break them up into sections. I'll go over each in detail.
1. Two Visualforce pages. The first provides the logic necessary to save the PDF to the contract, the second contains the logic for the PDF itself. When the first page loads, it calls up the second page, uses getContent() to render the PDF, then saves it as an attachment to the contract.
2. Messaging.SingleEmailMessage, mostly, just set the template ID, add the attachment from the prior step to the email's file attachments, and send it off. You'll probably want a page backing this logic so the user has a chance to cancel/make sure it looks correct/etc, plus you can't render PDFs from some contexts, in case you need to basically invoke (1) as part of this process.
3. In theory, you can't "automatically print" from the web. The user has to confirm that they want to print. It's just a matter of security. However, you can semi-automatically print. In a Visualforce page, call "window.open" to open a new window with the URL for the PDF, then call "print()" on the reference to the new window, and then "close()" the new window. Most browsers will happily follow this chain of command to one-click print your PDF; the Print dialog will appear and then the user can choose to print.
sfdcfox,
This is my VF page as it is currently.
Can you give me more details as to how to attach it to the contract as well as attach it to the email?
Thank you,
Steve Laycock
To attach to the contract, you need a second page that loads the first (the one you posted). You can do it like this:
Depending on if you're using the standard page or a custom page, your parameters will vary. I don't have time to go over the standard parameters right now, but the answer to that should already be on this forum.
sfdcfox,
It is a standard controller I'm using with this VF page PDF.
Sorry I'm a bit new to VF page management.
Can you please give me some more info? (For example, the code snippet you have given me, where would it go? A class, another VF page, someplace else???)
Any additional info will be highly appriciated and kudos as always will be given.
Thank you,
Steve Laycock
Yes, you'd create a new Visualforce page and just attach that to a button on the contract. As for emailing the attachment, you'll just need some URL hacking. I don't have the time to do the research on this, but try searching the forums. I'm sure someone has done something like that before.
Thanks SFDCfox!
I will let you know how it turns out.
Steve