Skip to main content
Participant
August 10, 2023
Answered

Javascript help with extract, saveAs, and maildoc

  • August 10, 2023
  • 1 reply
  • 840 views

Hello, I am have been trying to create a javascript that when a button is clicked the document will extract saveAs and then email the document. I have had sucess extracting the page, saveing it with a filename from one of the form fields. However i am running into a issue mailing the newly saved document. I am using a trusted funtion script.  I have reviewed the documentation out there however I just need a little help getting over the finish line. Here is what I have so far.

 

My Trusted script contained in a .js file named Myscripts that is located in my user folder.

var mySaveAs = app.trustedFunction(
   function(oDoc,cPath,cFlName)
   {
      app.beginPriv();
      // Ensure path has trailing "/"
      cPath = cPath.replace(/([^/])$/, "$1/");
      try{
         oDoc.saveAs(cPath + cFlName);
      }catch(e){
         app.alert("Error During Save");
      }
       app.endPriv();
   }
);

 Here is my Button Script: 

/*********** belongs to: AcroForm:Button1:Annot1:MouseUp:Action1 ***********/
var outputPath = "/c/temp";
var newFileName = "storedtickets " + this.getField("FORM_NAME").valueAsString + ".pdf";
var newDoc = mySaveAs(this.extractPages(0), outputPath, newFileName);
newDoc.mailDoc();

Everything works except for attaching the extracted page to the email. With this script i am getting the error "TypeError: newDoc is undefined
5:Field:Mouse Up"

If I change the newDoc.mailDoc() to this.mailDoc() or mailDoc() it is just attaching the orgnial file not the extracted page. Any help would be greatly apprecatied. 

This topic has been closed for replies.
Correct answer Matthew31589295zv7w

Thank you try67 that worked... it took me a few minutes to figure out where to put the oDoc, but this i how i modifed it and everything is working. 

var outputPath = "/c/temp";
var newFileName = "storedtickets " + this.getField("FORM_NAME").valueAsString + ".pdf";
var oDoc = this.extractPages();
oDoc.mySaveAs(oDoc, outputPath, newFileName);
oDoc.flattenPages(0);
oDoc.mailDoc();

1 reply

try67
Community Expert
Community Expert
August 10, 2023

Your mySaveAs method does not return any value, so newDoc is null. Change it to return a reference to the oDoc variable, and it should work.

Matthew31589295zv7wAuthorCorrect answer
Participant
August 10, 2023

Thank you try67 that worked... it took me a few minutes to figure out where to put the oDoc, but this i how i modifed it and everything is working. 

var outputPath = "/c/temp";
var newFileName = "storedtickets " + this.getField("FORM_NAME").valueAsString + ".pdf";
var oDoc = this.extractPages();
oDoc.mySaveAs(oDoc, outputPath, newFileName);
oDoc.flattenPages(0);
oDoc.mailDoc();