I already know how to create fields and how to read back their values after a prompt is created. However, I’m trying to dynamically create the fields, and I’m not sure how to read field values after the prompt that were dynamically created in a for-loop in the prompt.
Here’s my JSON:
{
"1": {
"name": "foo",
"creation date": "2019-12-24",
"priority": {
"name": "foobar",
"completion date": "",
"creation date": "2019-12-26",
"daily": [
{
"complete": false,
"date": "2019-12-27",
"task": "first task"
}
],
"enough": [
{
"date": "2019-12-28",
"description": "baz",
"enough?": true
},
{
"date": "2019-12-27",
"description": "qux",
"enough?": false
}
]
}
},
"2": {
"name": "bar",
"creation date": "2019-12-24",
"priority": {
"name": "bazqux",
"completion date": "",
"creation date": "2019-12-26",
"daily": [
{
"complete": false,
"date": "2019-12-27",
"task": "first task"
}
],
"enough": [
{
"date": "2019-12-28",
"description": "baz",
"enough?": true
},
{
"date": "2019-12-27",
"description": "qux",
"enough?": false
}
]
}
}
}
Here’s my script:
// Goals testing
// Get the JSON file
let fmCloud = FileManager.createCloud();
let file = fmCloud.readString("/Goals/goals.json");
let json = JSON.parse(file);
// // Prompt
/* --------------------- */
var today = Date.today().toString("dddd, MMM d");
var dStamp = Date.today().toString("yyyy-MM-dd");
var p = Prompt.create();
p.title = "Goals Today";
for (var goal in json) {
let goalName = goal;
let priority = json[goal].priority.name;
let toggle = goal+"toggle";
let field = goal+"field";
var label = goal + ":\nEnough today?";
p.addSwitch(toggle,label, false);
p.addTextView(field, priority,"", {height: "4"});
}
p.addButton("ok");
var con = p.show();
if (con) {
for (var goal in json){
let toggle = goal+"toggle";
let field = goal+"field";
alert(p.fieldValues[toggle] + p.fieldValues[field]);
json[goal]["enough"].unshift('{"date":dStamp,"description":field,"enough?":toggle}');
var output = JSON.stringify(json);
alert(output);
let success = fmCloud.writeString("/Goals/goals3.json",output);
}
}
else {
context.cancel();
}