Skip to content

Instantly share code, notes, and snippets.

@picsoung
Created April 16, 2024 10:15
Show Gist options
  • Save picsoung/d8710fef28ccec138ed33c09601b80d9 to your computer and use it in GitHub Desktop.
Save picsoung/d8710fef28ccec138ed33c09601b80d9 to your computer and use it in GitHub Desktop.
Airtable scripting to synchronize values from Airtable to Typeform dropdown
const TYPEFORM_API_KEY = "REPLACE_WITHYOURKEY"
const TYPEFORM_API_URL = "https://api.typeform.com"
const TYPEFORM_FORM_ID = "REPLACE_WITH_FORM_ID"
const TYPEFORM_FIELD_REF = "REPLACE_WITH_FIELD_REF"
let table = base.getTable('Teams');
// Query the table to retrieve records
let query = await table.selectRecordsAsync();
// Initialize an array to store the names
let choices = [];
// Loop through each record and add the name to the array
for (let record of query.records) {
let label = record.getCellValue("Name");
let ref = record.getCellValue("ref");
if (label && ref) {
choices.push({
ref,
label
});
}
}
// Log the array of choices
console.log(choices);
//GET current typeform definition
const formDef = await getTypeform(TYPEFORM_FORM_ID)
//find field and update it's choices
const field = formDef.fields.find((f)=> f.ref === TYPEFORM_FIELD_REF)
field.properties.choices = choices
console.log("updatedFormDef", formDef)
await updateTypeform(formDef)
async function getTypeform(formId) {
let requestOptions = {
method: 'GET', // Use PUT to update resource
headers: {
'Authorization': `Bearer ${TYPEFORM_API_KEY}`,
'Content-Type': 'application/json'
}
};
let response = await remoteFetchAsync(`${TYPEFORM_API_URL}/forms/${formId}`, requestOptions);
if (response.ok) { // Check if the response is successful
let result = await response.json();
console.log("get form successfully:", result);
return result
} else {
console.error("Failed to update form. Status:", response.status, "Response:", await response.json());
}
}
async function updateTypeform(formDef) {
console.log("in update typeform")
let requestOptions = {
method: 'PUT', // Use PUT to update resource
headers: {
'Authorization': `Bearer ${TYPEFORM_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(formDef)
};
let response = await remoteFetchAsync(`${TYPEFORM_API_URL}/forms/${TYPEFORM_FORM_ID}`, requestOptions);
if (response.ok) { // Check if the response is successful
let result = await response.json();
console.log("Form updated successfully:", result);
} else {
console.log("Failed to update form. Status:", response.status, "Response:", await response.json());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment