Hi,
I have written a script (below) which lets a user add records to Airtable via Drafts; however, I’ve run into two issues which I would appreciate some help with.
First of all, trying to set the Tags field in a new record within the Equipment base is not working. It is a multiselect field, and I suspect the API is not happy with what it’s being sent. Copying the result of this statement to the clipboard results in, e.g. ,tech, tools. It may be that the leading comma is confusing the API. I’ve discovered that adding {typecast:true}
should help, but I think I’ve put it where it belongs, and it doesn’t seem to solve the problem.
Finally, I can’t seem to get the script to write accurately to the console upon successful execution (i.e. if Airtable is called, either after the first prompt, or at the end of the script).
let basename = "";
let cb = CallbackURL.create();
cb.baseURL = "airtable://";
(() => {
loop:
while(true) {
let p = Prompt.create();
p.addButton("Add to existing record");
p.addButton("Create a new record");
if (!p.show()) {
context.cancel();
return;
}
switch (p.buttonPressed) {
case "Add to existing record":
app.setClipboard(draft.content);
cb.open();
break loop;
break;
case "Create a new record":
if (chooseBase()) break loop;
break;
}
}
})()
function chooseBase() {
loop:
while(true) {
let p = Prompt.create();
p.addButton("Add to Equipment");
p.addButton("Add to Inventory");
if (!p.show()) {
context.cancel();
return;
}
switch (p.buttonPressed) {
case "Add to Equipment": basename = "Equipment"; addRecord();
break loop; break;
case "Add to Inventory": basename = "Inventory"; addRecord();
break loop; break;
}
}
}
function addRecord(){
var options = ["accessories", "apple", "bagsandcontainers", "cables", "chargers", "children", "clothes", "healthandhygiene", "luxury", "photography", "stationery", "tech", "tools"];
var selectedOptions = [""];
var base = ATBase.create(basename);
var main = ATTable.create("Main", base);
var newRecord = ATRecord.create();
if (basename == "Equipment") {
let p1 = Prompt.create();
p1.addTextField("name", "Name:", "");
p1.addTextField("attachments", "Attachments:", "");
p1.addSelect("tags", "Tags:", options, selectedOptions, true);
p1.addSwitch("edc", "EDC:", false);
p1.addSwitch("purchased", "Purchased:", false);
p1.addTextField("notes", "Notes:", "");
p1.addButton("OK");
if (!p1.show()) {
context.cancel();
return;
}
newRecord.setFieldValue("Name", p1.fieldValues['name']);
newRecord.setFieldValue("Attachments", p1.fieldValues['attachments']);
newRecord.setFieldValue("Tags", p1.fieldValues['tags',{"typecast":true}]);
newRecord.setFieldValue("EDC", p1.fieldValues['edc']);
newRecord.setFieldValue("Purchased", p1.fieldValues['purchased']);
newRecord.setFieldValue("Notes", p1.fieldValues['notes']);}
else if (basename == "Inventory") {
let p2 = Prompt.create();
p2.addTextField("name", "Name:", "");
p2.addTextField("model", "Model:", "");
p2.addTextField("purchasedate", "Purchase Date:", "DD/MM/YYYY");
p2.addTextField("purchaselocation", "Purchase Location:", "");
p2.addTextField("warranty", "Warranty:", "");
p2.addTextField("serialnumbers", "Serial Number(s):", "");
p2.addTextField("notes", "Notes:", "");
p2.addTextField("manuals", "Manual(s):", "");
p2.addButton("OK");
if (!p2.show()) {
context.cancel();
return;
}
newRecord.setFieldValue("Name", p2.fieldValues['name']);
newRecord.setFieldValue("Model", p2.fieldValues['model']);
newRecord.setFieldValue("Purchase Date", p2.fieldValues['purchasedate']);
newRecord.setFieldValue("Purchase Location", p2.fieldValues['purchaselocation']);
newRecord.setFieldValue("Warranty", p2.fieldValues['warranty']);
newRecord.setFieldValue("Serial Number(s)", p2.fieldValues['serialnumbers']);
newRecord.setFieldValue("Notes", p2.fieldValues['notes']);
newRecord.setFieldValue("Manual(s)", p2.fieldValues['manuals']);}
main.addRecord(newRecord);
main.update();
cb.open();
return true;
if (success) {
console.log("Item added to Airtable");
} else {
console.log(cb.status);
if (cb.status == "cancel") {
context.cancel();
} else {
context.fail();
}
}
}
Thanks for any help you can offer,
Martin