Skip to main content
Participating Frequently
January 5, 2024
Question

JavaScript that will send form to recipients that are based on the value selected in a dropdown

  • January 5, 2024
  • 1 reply
  • 3747 views

I created a PDF form using Adobe Acrobat that is designed to capture information and then allow the user to send the completed form to the people who need the information using a "Send" button.

 

I am brand spanking new to making forms (newer to code) and the code below I got from research on the internet. The code is in the button properties under Actions and Run a Javascript. It is included here for reference. It works very well if the email recipients do not change.

```

var cToAddr = "Email1@work.com;Email2@work.com;Email3@work.com";
var cCCAddr = "CCMail@work.com";
var cSubLine = "[Student Name / Employee Number] Situation";
var cBody = "Enter other information as desired."
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});


```

Now I need the form to go to different recipients based on the value selected from a drop-down menu on the form. For example, if Team 1 is selected from the dropdown, then I need the form to go to Team1@email.com. If Team 2 is selected, then I need the form to go to Team2@email.com, etc.

 

I do not know how to modify this code to make this happen nor have I been able to find new code that would do this and need assistance.

 

I've read Thom Parker's November 4, 2013 post (Form sbubmit /eMail demystified) but am probably too new at this to get the answer that I need, though I have learned a little. 

 

Is there a way to modify the current code (above) or could someone provide the proper code to achieve this function? I am sure that it is possible, and probably quite simple, I just can't figure it out. 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
January 5, 2024

See: https://acrobatusers.com/tutorials/dynamically-setting-submit-e-mail-address

Basically, all you need to do is place the email address as the export value of each item in the drop-down and then just use that field's value in your code, like this:

 

var cToAddr = this.getField("Team").valueAsString;

Participating Frequently
January 6, 2024

So here is the code I have now (names changed to protect the innocent...)

var cToAddr = this.getField("Team").valueAsString;
var cCCAddr = "First.Last@work.com";
var cSubLine = "[Student Name / Employee Number] Not Recommended";
var cBody = "Enter other information as desired."
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});

I kind of understand now what this is doing. The Export Value for each of the four teams in the dropdown menu now have the actual emails for the individuals who need to receive the form. 

 

However, something is not working because the code no longer does the first step of opening the email...

 

What have I done incorrectly?

 

 

Participating Frequently
January 7, 2024

You must only use straight quotes in your code ( " ... " ), not curly ones ( “ ... ” ). Also, the comparison operator in JS is == (or ===). The single-character version ( = ) is used to assign a value, not compare it.

 

But my reply was actually about this:

var cSubLine = "[Student Name / Employee Number] Not Recommended";

If you have the name of the student in the form then you can insert it into that string, like this:

var cSubLine = this.getField("Student Name").valueAsString  + " Not Recommended";


AH, yes. Very helpful on both counts. I just did some reading on comparison operators and will do some more. 

 

In the meantime, I think this is close, or there may be a gross conceptual error, either way, it has not worked yet. 

 

var cToAddr = this.getField("Fleet").valueAsString;

if ("Fleet" == Team1) {
	var cCCAddr = "People1@Work.com";
} else if ("Fleet" == Team2) {
	var cCCAddr = "People2@Work.com";
} else {
	var cCCAddr = "";
}

var cSubLine = this.getField("Student Name").valueAsString + this.getField("Employee Number").valueAsString + "Not Recommended";
var cBody = "Enter other information as desired.";
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});