Convert Attachments to URLs

Convert attachment fields into comma-separated URL lists for integration with external services

Overview

This script processes attachment fields in your table and converts them into comma-separated URL strings. This is particularly useful when you need to send attachment URLs to third-party integration services that expect URL strings rather than attachment objects.

/**
 * Convert attachments to URLs
 *
 * Outputs comma-separated URLs for all attachments and writes them to another field for each record.
 * Can be used for sending URLs to third-party integration services.
 */

// Script configuration - allows users to select table, view, and fields
const config = input.config({
  title: '📎 Convert attachments to URLs',
  description: 'Output comma-separated URLs for all attachments and write to another field for each record. Can be used for sending URLs to third-party integration services.',
  items: [
    input.config.table('selectedTable', {
      label: 'Table',
      description: 'Select a table with records that have attachments you want URLs for',
    }),
    input.config.field('selectedField', {
      label: 'Attachment field from the selected table',
      parentTable: 'selectedTable',
      description: 'Select the attachment field from the table above you want URLs for'
    }),
    input.config.view('selectedView', {
      label: 'View from the selected table',
      parentTable: 'selectedTable',
    }),
    input.config.field('fieldToEdit', {
      label: 'Field to output the attachment URLs to',
      parentTable: 'selectedTable',
      description: 'A long text field is recommended. A comma-separated list of URLs will be generated here'
    })
  ]
});

// Assign selections to variables for the script
const selectedTable = config.selectedTable;
const selectedField = config.selectedField;
const selectedView = config.selectedView;
const fieldToEdit = config.fieldToEdit;

/**
 * Main function to convert attachments to URLs
 */
async function convertAttachmentsToUrls() {
  // Verify the selected field is an attachment field
  if (selectedField.type !== UITypes.Attachment) {
    output.text(`"${selectedField.name}" is not an attachment field. Run the script again with an attachment field.`, 'error');
    return;
  }

  // Load records from the selected view
  const query = await selectedView.selectRecordsAsync({
    fields: [selectedField]
  });

  // Get all records
  const records = query.records;

  // Array for records with attachments and their URLs
  const recordsToUpdate = [];

  // Process each record
  for (let i = 0; i < records.length; i++) {
    const record = records[i];
    const attachments = record.getCellValue(selectedField);

    // Only process records with attachments
    if (attachments && attachments.length > 0) {
      // Extract URLs from attachments
      const attachmentUrls = attachments.map(attachment => attachment.url);
      const urlString = attachmentUrls.join(', ');

      // Add to update array
      recordsToUpdate.push({
        id: record.id,
        fields: {
          [fieldToEdit.name]: urlString
        }
      });
    }
  }

  // Update records in batches of 50
  while (recordsToUpdate.length > 0) {
    const batch = recordsToUpdate.slice(0, 50);
    await selectedTable.updateRecordsAsync(batch);
    recordsToUpdate.splice(0, 50);

    // Show progress
    output.text(`Updated ${Math.min(50, batch.length)} records...`);
  }
}

// Run the script
await convertAttachmentsToUrls();

// Output message to indicate when the script is done
output.text('Records have been updated with attachment URLs', 'success');

How it Works

  1. Select Source Field: Choose an attachment field containing the files you want to convert
  2. Choose Target Field: Select a text field where the comma-separated URLs will be written
  3. Process Records: The script processes each record, extracting URLs from attachments
  4. Output Format: URLs are joined with commas: url1,url2,url3

Requirements

  • An Attachment field with uploaded files
  • A Text field to store the comma-separated URL output

Example

Before:

  • Attachment Field: [file1.pdf, image2.jpg, document3.docx]
  • URL Field: (empty)

After:

  • Attachment Field: [file1.pdf, image2.jpg, document3.docx] (unchanged)
  • URL Field: https://example.com/file1.pdf,https://example.com/image2.jpg,https://example.com/document3.docx