-
-
Save justinsbarrett/c304d84232a7cefba99b8d79dbd652a0 to your computer and use it in GitHub Desktop.
Copy data from "template" record to a new record, then clear the template record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Title: Copy template to new record | |
* Version: 1.0 | |
* License: MIT | |
* Author: Justin Barrett | |
* Sites: | |
* http://allaboutthatbase.tips | |
* https://www.youtube.com/c/AllAboutThatBase1 | |
* | |
* Revision history: | |
* 01/10/2022 1.0 Initial release | |
* | |
* Description: Copies all non-computed field values from a "template" | |
* record to a new record, then clears the contents of the template. | |
* | |
* Requirements: One custom field must be present | |
* - A single-select field used to trigger the automation. | |
* | |
* Instructions: Edit the configuration section below to set up the system | |
* based on the design of your base. | |
* | |
*/ | |
// ------------------------ START CONFIGURATION ------------------------// | |
// The name of the table on which to operate. | |
const tableName = "Contacts" | |
// The name of the single-select field used to indicate the action to take. | |
const actionFieldName = "Option" | |
// The name of the single-select choice marking the template record | |
const templateName = "Template" | |
// ------------------------- END CONFIGURATION -------------------------// | |
const table = base.getTable(tableName) | |
const {recordId} = input.config() | |
const actionField = table.getField(actionFieldName) | |
const fieldExceptions = [actionFieldName] | |
const main = async() => { | |
const newRecord = await table.selectRecordAsync(recordId, {fields: table.fields}) | |
if (!newRecord) { | |
console.log("❌ New record not found. Aborting.") | |
return | |
} | |
// Create a new record with all of the data from the template | |
const newData = {} | |
const clearData = {[actionFieldName]: {name: templateName}} | |
for (let field of table.fields) { | |
// Skip computed fields or exceptions | |
if (field.isComputed || fieldExceptions.includes(field.name)) { | |
continue | |
} | |
newData[field.name] = newRecord.getCellValue(field) | |
clearData[field.name] = undefined | |
} | |
await table.createRecordAsync(newData) | |
// Reset the template record | |
await table.updateRecordAsync(newRecord, clearData) | |
} | |
await main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment