When an opportunity meets certain criteria, I need to generate a PDF and attach it to the opportunity record. While it works on existing records, I'm running into a problem on new ones.

How I'm doing it:

  1. Whenever an opportunity is created or updated the Process Builder launches a Visual Flow
  2. The Flow configures a parameter string (couldn't do it in PB) and calls an apex module.
  3. The apex module renders a VF page generates the PDF
  4. The page's controller attaches the PDF to the opportunity.

Though a bit round-abut this is working perfectly for existing opportunities and gets me around the limitation that triggers can't generate PDFs.

However, it's crashing on new opportunities. I've determined the cause to be that the Attachment object requires the ParentID field represent a known object. While I do have the Object's ID (more specifically, what it will be), it's not yet committed to storage so inserting the new attachment doesn't recognize it and crashes. (doing a query for the opportunity by id yields zero results)

  • Any suggestions on how to get out of this knot?

  • I'm considering using a scheduled job so it unlinks the activities
    from one another - is there a way to schedule PB scripts or Flows?

  • Would getting into the pilot for Workflows calling Flows solve this
    timing issue? (I suspect not)

1 Answer 1


I've solved this and want to post how it got around the problem in case others run into a similar issue in the future.

  1. Instead of trying to attach the PDF to the Opportunity, I put the PDF in a document folder (using apex), with the opportunity ID it's associated with stored in the metadata and a unique string that indicates it's one of my "temporary stash" files.
  2. I then created a separate apex class that searches for all documents that contain my unique string mentioned above. I then attach each pdf to it's target object id (stored in metadata) then delete it from documents.
  3. Finally, I add a second action within the Process Builder script that calls the new method.

Since the second PB action runs in a separate execution context, all data from the previous context has been fully stored and the apex method can see the new opportunities. With this, the timing issue evaporates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.