Help

The Airtable Community will undergo scheduled maintenance on September 17 from 10:00 PM PST to 11:15 PM PST. During this period, you may experience temporary disruptions. We apologize for any inconvenience and appreciate your understanding.

the script is not taking input as want to update values in linked field

1148 2
cancel
Showing results for 
Search instead for 
Did you mean: 
bharatsingh9808
4 - Data Explorer
4 - Data Explorer

the script is not taking input as want to update values in linked field


code:

const targetAudienceTableName = "Target Audience SS";

// Access the record ID from the triggering record
const triggeringRecord = input.config();
const recordId = triggeringRecord.RecordModified;
console.log("Triggering Record ID:", recordId);
const targetAudienceTable = base.getTable(targetAudienceTableName);
// Use the selectRecordsAsync method to retrieve the specific record
const recordDetails = await targetAudienceTable.selectRecordsAsync();
// Assume recordDetails is an array of records obtained from Airtable
const filteredRecords = recordDetails.records.filter(record => (
    record.getCellValue("Record ID") === recordId
));

// Check if the filteredRecords array is not empty
if (filteredRecords.length > 0) {
    const targetAudienceRecord = filteredRecords[0];

    // Get the values from the Lookup fields
    const newslettersMediaCompanyValues = targetAudienceRecord.getCellValue("Newsletters Media Company");
    const podcastMediaCompanyValues = targetAudienceRecord.getCellValue("Podcast Media Company");
    const youtubeMediaCompanyValues = targetAudienceRecord.getCellValue("YouTube Media Company");

    console.log("Newsletter", newslettersMediaCompanyValues);
    console.log("Podcaast", podcastMediaCompanyValues);
    console.log("YouTube", youtubeMediaCompanyValues);

    // Combine values
    const allMediaCompanyValues = [
        ...(newslettersMediaCompanyValues || []),
        ...(podcastMediaCompanyValues || []),
        ...(youtubeMediaCompanyValues || []),
    ];

    // Unique linked record IDs
    const uniqueLinkedRecordIds = new Set();

    for (const value of allMediaCompanyValues) {
        // Add the 'id' property to the Set to ensure uniqueness
        uniqueLinkedRecordIds.add(value.id);
    }

    // Convert the Set to an array with objects having 'id' property
    const linkedRecords = Array.from(uniqueLinkedRecordIds).map(id => ({ id }));
    console.log("linkedrecord", linkedRecords);

    // Log the unique values to the console for debugging
    console.log(`Record ID: ${targetAudienceRecord.id}`);
    console.log(`Unique Values: ${linkedRecords}`);

    // Update the "Media Company" linked field
    await targetAudienceTable.updateRecordAsync(targetAudienceRecord, {
        "Media Company": linkedRecords,
    });

    console.log("Update completed.");
} else {
    console.log("Record not found.");
}

 
Output:

CONSOLE.LOG

  1. "Triggering Record ID:"
  1. "recFR6bmsXDr0MlxQ"

CONSOLE.LOG

  1. "Newsletter"
  1. (10) ["rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2"]

CONSOLE.LOG

  1. "Podcaast"
  1. null

CONSOLE.LOG

  1. "YouTube"
  1. null

CONSOLE.LOG

  1. "linkedrecord"
  1. (1) [Object]

CONSOLE.LOG

  1. "Record ID: recFR6bmsXDr0MlxQ"

CONSOLE.LOG

  1. "Unique Values: [object Object]"

ERROR

Error: Field "fldFcoeRwS2SiTbLN" cannot accept the provided value.
    at main on line 58
2 Replies 2

Hey @bharatsingh9808

Give this a shot and lemme know how this works.
I'm not able to fully test it in a test environment, but the bones are solid:

const { recordId } = input.config();

const tables = {
    audience: base.getTable("Target Audience SS")
};

const fields = {
    recordId: "Record ID",
    newsletter: "Newsletters Media Company",
    podcast: "Podcast Media Company",
    youtube: "YouTube Media Company",
    media: "Media Company"
};

const trigger = await tables.audience.selectRecordsAsync({ fields: Object.values(fields) })
    .then((q) => q.records.find(r => r.id === recordId));

let lookupFieldValues = {
    newsletter: trigger.getCellValue(fields.newsletter)?.map(ref => ref.id) || [],
    podcast: trigger.getCellValue(fields.podcast)?.map(ref => ref.id) || [],
    youtube: trigger.getCellValue(fields.youtube)?.map(ref => ref.id) || []
};


let updateValue = (() => {
    
    let idValues = Object.values(lookupFieldValues).flat();

    if (idValues.length)
        return [...new Set(idValues)].map(value => ({ id: value }));
})();

if (updateValue) 
    await tables.audience.updateRecordAsync(trigger, { [fields.media]: updateValue });

A quick note that I forgot to include...

You have the following lines in your script:

const triggeringRecord = input.config();
const recordId = triggeringRecord.RecordModified;

For the script I just posted, you'll want to rename your `RecordModified` input variable to just `recordId`.