Help

CSV Automations Help

Topic Labels: Automations Data
130 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_Macklin
4 - Data Explorer
4 - Data Explorer

Can anyone figure out why im getting the following? My goal is when a record is updated with a CSV file, I want to convert the CSV file into a list of records that I can then use elsewhere. This is in conjunction with an automation.

CONSOLE.LOG
"Script started"
CONSOLE.LOG
"Record ID from input config:"
undefined
CONSOLE.ERROR
"No record ID provided in input config."


CODE BELOW SHOWN BELOW


console.log('Script started');

let tableName = 'Fabrication Package Members';
let attachmentFieldName = 'Assembly List';

// Function to parse CSV text into an array of objects
function parseCSV(csvText) {
    const lines = csvText.split('\n');
    const headers = lines[0].split(',').map(header => header.trim());
    const records = lines.slice(1).map(line => {
        const values = line.split(',').map(value => value.trim());
        return headers.reduce((record, header, i) => {
            record[header] = values[i];
            return record;
        }, {});
    });
    return records.filter(record => Object.values(record).some(value => value)); // Filter out empty records
}

// Main function to import CSV from attachment
async function importCSVFromAttachmentField(recordId) {
    try {
        // Validate the record ID
        if (!recordId) {
            throw new Error('Record ID is not provided');
        }

        // Get the table
        let table = base.getTable(tableName);

        // Get the record
        let record = await table.selectRecordAsync(recordId);
        if (!record) {
            throw new Error('Record not found');
        }

        // Get the attachment field value
        let attachmentField = record.getCellValue(attachmentFieldName);
        if (!attachmentField || attachmentField.length === 0) {
            console.log('Attachment field is empty');
            return;
        }

        // Get the URL of the attachment file
        let attachmentUrl = attachmentField[0].url;

        // Fetch the CSV file
        let response = await fetch(attachmentUrl);
        if (!response.ok) {
            throw new Error(`Failed to fetch CSV file: ${response.statusText}`);
        }
        let csvText = await response.text();

        // Parse the CSV text into an array of objects
        let records = parseCSV(csvText);

        // Insert the records into the base
        for (let rec of records) {
            await table.createRecordAsync(rec);
        }
        console.log(`Imported ${records.length} records from attachment field.`);
    } catch (error) {
        console.error('Error importing CSV:', error);
    }
}

// Get the record ID from the automation input
let inputConfig = input.config();
let recordId = inputConfig.recordId;
console.log('Record ID from input config:', recordId);

// Ensure record ID is provided
if (recordId) {
    await importCSVFromAttachmentField(recordId);
} else {
    console.error('No record ID provided in input config.');
}



0 Replies 0