Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
18

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

Explorer ,
Jan 05, 2024 Jan 05, 2024

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. 

TOPICS
How to , JavaScript , PDF , PDF forms
2.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2024 Jan 05, 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2024 Jan 05, 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?

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2024 Jan 05, 2024

Screenshot 2024-01-05 at 22.11.28.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2024 Jan 06, 2024

Is that field actually named "Team"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2024 Jan 06, 2024

The dropdown is named Fleet. I am substituting "Team1" and so on here for the actual organization name. Each team has two individuals who need to receive the form once filled out, represented here by Team1A and Team1B in the Export Value. 

 

The following picture should illustrate it better. Sorry for the confusion and thanks for sticking with me!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2024 Jan 06, 2024

Screenshot 2024-01-06 at 08.33.33.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2024 Jan 06, 2024

Okay, I just got your point. In my attempt to keep emails private and what-not I goobered up the code and put "Team" where "Fleet" should have been. I do believe it's working now. I will play with it a bit to make sure. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2024 Jan 06, 2024

Yep, that did it! Thank you very much for your assistance, try67!!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2024 Jan 06, 2024

Glad to hear it!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2024 Jan 06, 2024

PS. You can use this method to pre-populate the subject line as well, if that information is available in a form field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 07, 2024 Jan 07, 2024

Interesting that you brought that up.

 

Trying to keep the form as simple as possible, I started experimenting with if, else if statements to populate the CC line based on those same selections. I tried the following but keep getting a SyntaxError: missing ; before statement.

 

With "Fleet" being the name of the dropdown...

 

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

 

Of course, if there is a better way to address this I am all ears. With the "To" addresses in the Export Value property, this was the only workaround I could think of based on my limited experience.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2024 Jan 07, 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";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 07, 2024 Jan 07, 2024

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});

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2024 Jan 07, 2024

You do a bit, yes... Replace the first part with this:

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


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

 

You have to keep in mind that anything you put in quotes is a literal string of text, and anything not in quotes is a variable's name.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 08, 2024 Jan 08, 2024

So that made things work pretty well. Oddly, it puts the "People1@Work.com" email in the TO field of the the email, not the CC line. Not a big deal as the correct people are receiving the email. Weird, though...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2024 Jan 09, 2024

That's odd. Can you post the full code? Also, what email application are you using?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2024 Jan 09, 2024

I use Outlook as the default emil application.

 

Here is the whole code:

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

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

if (fleet == "Team1") {
	cCCAddr = "Team1A@Work.com";
} else if (fleet == "Team2") {
	cCCAddr = "Team2A@Work.com";
} else {
    cCCAddr = "";
}
var cSubLine = this.getField("Student Name").valueAsString + " / " + this.getField("Employee Number").valueAsString + " / " + this.getField("Event").valueAsString + " " + this.getField("Result").valueAsString;
var cBody = "Enter other information as desired.";
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2024 Jan 09, 2024

What are the values of the Fleet field? How can they be used as an address if they are "Team1", "Team2", etc.? Something here doesn't make sense.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 12, 2024 Jan 12, 2024

In the Fleet dropdown, Team1, Team2, and Team3 are the options. Each option has an export value that fills in the TO line of the email. Team3 needs no CC input. I thought the "if, else if, else" code would then fill out the CC line based on those selections. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 12, 2024 Jan 12, 2024

So some items have email addresses as their export values, and others don't?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 13, 2024 Jan 13, 2024
LATEST

They all have emails as export values that fill in the To Address. I tried to use the if, else if... argument to fill out the CC line based on the value selected from the "Fleet" dropdown item selected, being one of the three teams. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines