Generate a PDF and Attach it to record from a trigger in Salesforce

Salesforce, junglified.

The reason you stumbled upon this post is because you tried using the .getContentAsPdf() method in a trigger and you got an error message stating you cannot use it in a trigger. So you tried to search and figure out as to how you could do it.

I had faced a similar issue when a task was given to me attach a pdf(which would be automatically created) when a record is created. Inititally I thought its quite simple and accepted it. But when I actually started coding, I became aware of the issues with this requirement.

Well, I googled for a while and came across a post (unfortunately, I could not find it now) which had the solution to this problem, but the guy had not posted the code to demonstrate. So I thought I could share the code in a post as this could help someone.

Here you go:

View original post 362 more words