Passing values of prompt actions to next step in an email action step

I am new to scripting and this is my first JavaScript script ever.
I’m trying to create an action that asks for a date and passes that on to the subject and body of an email.
I can’t seem to retrieve the value of the datepicker.
What am I doing wrong?

The code in the script step looks okay from a quick glance (but I have not entered it and confirmed this), but you have tried to use code in the Mail step. Only the Script step supports code.

Typically the content of a draft might become the content of an email, like in this action which also uses scripting and a mail action.

However, you can also create and populate custom template tags that can then be used in the Mail step.

Hopefully, that will get you back on the right track.

Also, remember you can share a link to the action so others can download it and check it. It saves others from having to recreate your whole action from scratch from screenshots.

Thanks so much for the help here. I’ve gotten this to work but I now need to format the date as YYYY-MM-DD

Also, how do I assign the variable the date without the if ( block being invoked? I know that the only thing it’s doing is showing an alert.

In the next step, I am just calling the [dateBegin] tag.

Also, because I want to learn JavaScript, I wanted to know if there is a way to do this without using tags and just inserting the variable directly in the subject and body of the email?

var p = Prompt.create();
p.title = "When is the meeting";
p.addDatePicker("dateBegin", "Begin date", new Date(), {
  "mode": "date"
var startDate = p.fieldValues["dateBegin"]

if (
	if (p.buttonPressed == "OK")
		alert("Begin date is " + p.fieldValues["dateBegin"]);
draft.setTemplateTag("date", p.fieldValues["dateBegin"]);

A couple of notes/answers:

  • Your script should not try to get the fieldValues from the prompt until after show() is run. Those values are not the user selected values until the prompt has been display and, well, the user selects them. So move the var startDate... insert the if block, like:
if ( {
    var startDate = p.fieldValues["dateBegin"]
    // ...
  • When you script a prompt, it’s not automatically creating and tags or values that are accessible in other steps.
  • The fieldValue returned when using a date picker is a Date object. You will need to manually format that date to a string if you need if to be in a specific format. You can use the strftime function for that purpose, like:
var dateString = strftime(p.fieldValues["dateBegin"], "%Y-%m-%d")
draft.setTemplateTag("date", dateString);
  • You can pass values to a Mail action step with setTemplateTag as you are doing, or you can use the Mail object to create and send mail in script.