auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
// Get Group Name from Slack Message
var msg = steps.trigger.event.text;
var n = msg.indexOf("* in ");
var group = msg.substr(n+5, msg.length - n+5);
//console.log('https://api.airtable.com/v0/appsEhmLOfUP2Svk5/MGL%20Groups?filterByFormula=SEARCH("'+group+'",{Group})');
const config = {
url: `https://api.airtable.com/v0/appsEhmLOfUP2Svk5/MGL%20Groups?filterByFormula=SEARCH("${group}",{Group})`,
headers: {
Authorization: `Bearer ${auths.airtable.api_key}`,
},
}
//steps.getGroup.$return_value.records[0].fields.baseid
var x = await require("@pipedreamhq/platform").axios(this, config);
var baseID = x.records[0].fields.baseid;
return baseID;
//return await require("@pipedreamhq/platform").axios(this, config);
//var results = await require("@pipedreamhq/platform").axios(this, config)
//console.log(results);
//var baseID = results.records[0].fields.baseID;
//return baseID
//return steps.getGroup.$return_value.records[0].fields.baseid
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
// You'll need to add your Airtable API key and baseId to the
const Airtable = require('airtable')
const base = new Airtable({apiKey: auths.airtable.api_key}).base(steps.getBaseID.$return_value)
// Used for extracting the baseId and recordId from the Airtable URL
// included in each record update sent to Slack
const titleRegex = /https:\/\/airtable\.com\/(?<tableId>.*)\/(?<recordId>.*)/
// Airtable sends a batch of updates in a single message, but includes
// updates to individual records in Slack message attachments.
// For each of those attachments (record updates), we construct a JavaScript
// object that looks closer to what you'd expect to receive from a webhook directly.
event.updates = [];
for (let attachment of steps.trigger.event.attachments) {
let { tableId, recordId } = attachment.title_link.match(titleRegex).groups
//console.log(`tableId: ${tableId}, recordId: ${recordId}`)
let record = await base(tableId).find(recordId)
let createdTime = record._rawJson.createdTime
let baseId = steps.getBaseID.$return_value
let updates = attachment.fields.map(slackMessageField => {
return {
field: slackMessageField.title,
newValue: slackMessageField.value,
}
})
event.updates.push({
record, baseId, tableId //, recordId, createdTime, updates
})
}
return event.updates;
//return record;
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
records = [];
updates = [];
var ab;
var status;
var notes;
for (let mglUpdates of steps.getUpdateData.$return_value) {
if(!mglUpdates.record.fields.ab) {
ab = '';
} else {
ab = mglUpdates.record.fields.ab;
}
if(!mglUpdates.record.fields.AutoStatus) {
status = '';
} else {
status = mglUpdates.record.fields.AutoStatus;
}
if(!mglUpdates.record.fields.Notes) {
notes = '';
} else {
notes = mglUpdates.record.fields.Notes;
}
if(!mglUpdates.record.fields["Date Accepted"]) {
mglUpdates.record.fields["Date Accepted"] = null;
}
if(!mglUpdates.record.fields["Date Returned"]) {
mglUpdates.record.fields["Date Returned"] = null;
}
if(!mglUpdates.record.fields["Date Dispatched"]) {
mglUpdates.record.fields["Date Dispatched"] = null;
}
updates.push({
fields: {
"accepted by": ab,
"status": status,
"notes from maker": notes,
"accepted by maker": mglUpdates.record.fields["Date Accepted"],
"received from maker": mglUpdates.record.fields["Date Returned"],
"sent to dispatch": mglUpdates.record.fields["Date Dispatched"]
},
id: mglUpdates.record.fields["source rid"]
})
}
records.push.apply(records,updates);
console.log('records',records);
if (records[0].id === undefined) {
console.log('no source id');
$end("No records to update")
} else {
console.log('source id exists')
return records;
}
An array of update objects. Each of these objects should have two properties: 1) fields, which contains all of your record's cell values by field name, and 2) id, the ID of the record in Airtable you'd like to update. You can include all, some, or none of the field values.
Your Airtable Base ID
The name of your table in Airtable
async
(params, auths) => {
}
const chunk = require('lodash.chunk');
const BATCH_SIZE = 10; // Airtable API allows to update up to 10 rows per request.
const { records } = params
if (!records || !records.length) {
console.log("No records found")
return
}
const size = records.length;
const response_records = []
async function updateRecords(data){
const config = {
method: "patch",
url: `https://api.airtable.com/v0/${params.base_id}/${encodeURIComponent(params.table_name)}`,
headers: {
Authorization: `Bearer ${auths.airtable.api_key}`,
},
data
};
return await require("@pipedreamhq/platform").axios(this, config);
};
const records_sets = chunk(records, BATCH_SIZE);
for (const records_set of records_sets) {
response_records.push(await updateRecords({ records: records_set }));
}
return response_records;