I am trying to use an inbound email handler to extract a PDF attachment, and then parse out all of the PDF text and save it on a field. The way I am currently doing this is by using a 3rd party JavaScript library and parsing via JavaScript.

This leads me to my question- Can I use this same JavaScript in an apex class that has no VF page attached?

I have figured out that you cannot explicitly use JavaScript in Apex, and other possibilities I looked into was creating a dummy VF page that pretty much just does this and calling it from the Apex, but the only examples of this that I could find were using the Test.setCurrentPage which wont work in a regular Apex class.

EDIT: Since many are asking, I am using an external JavaScript Library specifically to iterate over PDFs and extract the PDF text, so that I can parse through and pick out specific information.

Example- Customer has a work order or some other type of document they generate outside of our Salesforce product.

  1. Customer e-mails work order PDF as an attachment to an e-mail handler.
  2. E-mail handler (apex class) takes PDF and uses JavaScript Library to extract text from PDF.
  3. Apex then parses through information to get out necessary data and save it or do something with it.

We have 1 and 3 handled, all I need to be able to do is access the JavaScript and JavaScript library in 2 in my apex code somehow so that I can pull the text out of the PDF.

A few possibilities I have thought about-

  • Render a blank VisualForce page to run script from e-mail handler. I'm unsure if this is possible to instantiate from an apex class.
  • Use some sort of tool to run the JavaScript externally from the Apex Class, and I am absolutely clueless on where to begin with this.
  • Use the JavaScript inside the Apex. This was my first thought, but is impossible as I understand.
  • 4
    The short and long answer is no. Apex is its own language that cannot have any JS embedded. I am not sure what your use case is, but if you had the opportunity to add a VF page to your process, then there are several methods to make Apex and JS interact with each other. If you comment here about your use-case I can try and stub something for you – Sebastian Kessel Nov 20 '17 at 18:09
  • @SebastianKessel The use case is trying to run this as an inbound e-mail handler. I'm relatively new to apex (about 3-4 months of experience), so I'm not sure if there is a way to make a VF page that could be called when the inbound e-mail handler class triggers that wouldn't display or anything, just process the script with the Apex data. That would work. – Josh Schoenbachler Nov 20 '17 at 18:20
  • Why do you need JS? What are you trying to accomplish with it? You may not even need it.... – Eric Nov 20 '17 at 18:33
  • What are you trying to do exactly. What's the input and the output? – Sebastian Kessel Nov 20 '17 at 18:36
  • You could use callouts to an external service (for example set one up on Heroku). – Adrian Larson Nov 20 '17 at 18:53

Eric and Adrian both had a possibility which is what I'm going to have to make work. I'm going to set up a Force.com page and use a callout from the apex class to run the JavaScript on that page.

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.