Outputting the contents of a prompt action

I am new to Actions and know almost nothing about JavaScript, and I am struggling to understand some of the documentation (and how various elements link to each other)

I wish to a create a prompt which

  1. lets me choose the method by which a client has contacted me [contact];
  2. the date they did so [when]; and
  3. add some text about the nature of the contact [activitiy].

and then outputs the text into an existing draft (at the cursor position) as follows:

[when]: [contact] - [activity]

I believe I have successfully created the prompt and capturing the necessary data (see below).

But I cannot work out from the documentation how to output the captured data.

I would be grateful for help in doing so.

var vcontact = ["email from", "email to", "call from", "call to"];

var p = Prompt.create();

p.title = "Data entry";

p.message = "Time entries for notary work";

p.addTextView("activity", "Activity", "", {"autocapitalization":"none","height": 100});

p.addSelect("contact", "Type of contact", vcontact, [""], false);

p.addDatePicker("when","When", new Date(), {"mode":"date"});

// p.addTextField("task", "Task", task, {"autocapitalization":"sentences"});

p.addButton("Add data to the draft");

var con = p.show();

The following line will insert the values from your prompt, in the format specified, into the current draft, replacing any selected text. fieldValues as you’ll see is what’s required and it is just using the identifiers that you specified in order to reference each element.

editor.setSelectedText(p.fieldValues["when"] + ": " + p.fieldValues["contact"] + " - " + p.fieldValues["activity"]);

I suspect you’ll probably want to take this a little further in terms of date/time formatting, and cursor positioning after the data has been inserted.

Sylumer,

Thank you very much. You are right about wanting to take the action further regarding date formatting and position of the cursor.

I would be grateful to know how to turn “Mon May 27 2019 23:24:49 GMT+0100 (BST)” to “27/05/19”. I have seen the description of [[date|format]] on this page but not sure how to make it work for a data captured in a variable.

Also how do I position the cursor at the end of the text.

Any help would be gratefully appreciated.

If you take a look at the Prompt documentation, and in particular, the Add Date Picker section, you’ll note that the value returned in fieldvalues is a JavaScript date object.

This page that I found as the top result on a quick Google search seems to break down the key options nicely.

Whilst that’s a good grounding for understanding the date option formatting options a bit better it is also very useful to know that back in version 5.4, a global strftime function (string format time) was added. You can find more details on the tokens for this at strftime.org, but it is what is used for the template tag formatting, so you may already be familiar with it.

The editor documentation has a couple of notable functions for this. getSelectedRange tells you where your selection starts and how long it is. setSelectedRange is similar but specifies the selection you want to put in place. If you want to start at the end, you need to push the selection to the position specified by the original start plus the length, and have a length of zero - nothing selected.

1 Like

I am grateful for the further reply, but I am very obviously struggling to translate it to my situation (who knows almost nothing about JavaScript).

I can see in the Prompt documentation that Add Date Picker is a JavaScript date object and that use of the strftime function was added - but looking at the various documentation, none of this tells me how to take the result of a variable and format the date in the way I want (or rather the syntax to do so). For example, there is nothing on this page or the link there to Dr Drang post which indicates how to use format the output of a variable.

I have looked at https://codehandbook.org/javascript-date-format/ which you linked and tried to follow what it is suggesting by

adding the following lines:

var vdate = when.getFullYear(0) + "-" + (when.getMonth()+1) + "-" + when.getDate();

editor.setSelectedText(p.fieldValues["vdate"] + ": " + p.fieldValues["contact"] + " - " + p.fieldValues["activity"]);

But get the following error

“Script Error: ReferenceError: Can’t find variable when”

So I am obviously not doing something right.

Some some further guidance would be really appreciated.

Okay, based on the above, see if this helps you with what I was suggesting.

//Insert includes use of strftime function to format date object to dd/MM/yy for output
editor.setSelectedText(strftime(p.fieldValues["when"],"%d/%m/%y") + ": " + p.fieldValues["contact"] + " - " + p.fieldValues["activity"]);

//Get start/end in draft of inserted (current) line
let lineRange = editor.getSelectedLineRange();

//Set new selection to be end of current line and zero length
editor.setSelectedRange(lineRange[0] + lineRange[1], 0);