Using prompt result in New Draft with Template

(This has been asked a year ago, but no answer I can work with, so please forgive me if ask it again.)

I am using several prompts to read some data, then I insert these in a template. All works well.

Now I’d like to read a prompt via JavaScript like so:

// Prompt example
// Using Select 

var p = Prompt.create();
p.title = "Select Prompt Example";
p.message = "Demonstrates options for single and multi-option select in prompts.";

var options = ["Mike", "Ugur", "Lilly"];
var selectedOptions = ["Ugur"];

// single selection
p.addSelect("s1", "Select one...", options, selectedOptions, false);

p.addButton("OK");

if (p.show()) {
	var s = "Selected: " + p.fieldValues["s1"] + "\n\n";
	alert(s);
}

and use the result in my template.

How do I reference the result of the above code in a template?

You need to create your own template tag. After you set your s variable, assign it to a template tag (e.g., selected_option).

d.setTemplateTag("selected_option", s);

You can then use the template tag in your template.

1 Like

Thank you for the quick reply. - But d need to be created? Or how’d I assign the current draft to d?

Apologies for the very basic questions, I just started using drafts, coming from Emacs + Elisp, this is all very new to me.

1 Like

Sorry, that’s my mistake😖, and you are absolutely spot on about the original assignment making no sense.

d should be draft, which is an inbuilt variable that refers to the current draft.

draft.setTemplateTag("selected_option", s);
1 Like

Excellent! That worked. Thank you very much!

Here is the working version:


var person = Prompt.create();
person.title = "Fuer wen ist die Rechung?";
person.message = "Waehle Person aus.";

var options = ["Mike", "Ugur", "Lilly"];
var selectedOptions = ["Ugur"];

// single selection
person.addSelect("s1", "Select one...", options, selectedOptions, false);

person.addButton("OK");

if (person.show()) {
	var s = "Selected: " + person.fieldValues["s1"] + "\n\n";
	var name = person.fieldValues["s1"];
	alert(s);
	draft.setTemplateTag("person_name", name);
}

In my template I reference the tag as [[person_name]].

1 Like

For what it’s worth, if you aren’t using name for other things, you could just consolidate to one line.

draft.setTemplateTag("person_name", person.fieldValues["s1"]);
1 Like

Probably a basic question but when does that template tag go away? At the end of the script or action? Or when you delete it manually?

It exists from the point you create it to the end of the action execution run.

I’ve never seen anything to “delete” one, or had a reason to want to explicitly delete one, but you could null it if you needed to - e.g. If you wanted to only populate in certain conditions via a script, but always include it in some final action step template.

1 Like