+ Start a Discussion

Call Apex class from Workflow/Approval rules

Hi Everyone,


The project I'm currently working on requires that whenever a new record (SLS , a custom object) is a created and after it gets approved by a manager a pdf version of the record should generated, a case object should be created and the pdf will be attached to that case object and will be sent over using salesforce2salesforce connection.


I have written the code to do this (pdfCreator class), but the problem is how do I call this class?


The pdf creation, case record creation and the attachmet should happen after the SLS record is approved (uses approval rules)  by a manager. So the event that should cause this class to run ( however indirect that may be) is the approval's final actions.


I can't use a trigger (After update on SLS by updating a dummy field on SLS ) to call pdfCreator as getContent() can't be called using triggers. Is there any other way that I can call this class (pdfCreator) that creates case object, generates pdf version of SLS and attaches it to case object and sends it over via sfdc2sfdc.


Thank you,


Ritesh AswaneyRitesh Aswaney

I wondered if invoking it via a @future method changes anything, but this post seems to suggest it doesnt change much.


I'd still give it a try - both getContent and getContentAsPdf.


As a workaround I would create a 'queue' table, which you can insert a record into, in the trigger. Also possible just to set a 'Ready for PDF' checkbox on the Record itself rather than a dedicated queue object.


Then have a scheduled batch job which looks at records inserted in this table or selects records by the checkbox you've set, generate these pdf docs, attach to the case, and the update the status to generated, so that subsequent runs dont pick them up.




@Ritesh, Thank you for the reply.


I'll try out the future call but I'm not sure how useful that would be.


As for your workaround, I thought triggers, schedule and batch apex classes cannot use getContent? can you or someone else clarify this?

Greg RohmanGreg Rohman



If your Visualforce PDF page is using a standard controller, this method of creating a Visualforce email template might work for you.




I'm currently looking for a way to email a PDF on a schedule, and the limitations with using getContentAsPdf has stumped me.