Nov 05, 2022 12:34 PM
I know this has been asked and answered, but I’m still confused about what I’m doing wrong.
2 tables, ‘Cust.’ & ‘Vehicles’.
(1) Cust can have (many) vehicles
(1) Vehicle can have only (1) Cust.
I have the automation setup to add the ‘Cust. Name’ to the ‘Vehicles’ table, and then add the ‘Vehicle’ to the ‘Cust.’ table.
ISSUE: Any customer who has more than one Vehicle is having the previous ‘Vehicle’ overwritten by the new one!?!?
ASK: Please tell me how to fix this… (screenshots to follow)
Thanks,
dg
Dec 03, 2022 08:47 PM
Are you using Scripting in your Automation? The trick is to use the "Spread" operator on the previous array within the creation of a new array that contains additional elements.
Example below;
let myCarsModels = ["BMW", "Volvo", "Holden"];
let nextCar = "Ford"
let allCars = [nextCar, ...myCarsModels];
console.log(allCars)
Juggling arrays, you may also find the problem of having an array within an array - something like this - note how "Ford" is nested within its own Array element within the allCars array;
To correct this, you can use the flat() method;
let myCarsModels = ["BMW", "Volvo", "Holden"];
let nextCar = ["Ford"]
let allCars = [nextCar, ...myCarsModels].flat();
console.log(allCars)
Which will return the desired array of cars;
[ 'Ford', 'BMW', 'Volvo', 'Holden' ]
Similarly, an array of Airtable Linked Field values can be manipulated in the same way as the string data.
let myCarsModels = [
{ id: "recABC123", name: "BMW" },
{ id: "recDEF456", name: "Volvo" },
{ id: "recGHI789", name: "Holden" }
];
let nextCar = [{ id: "recJKL010", name: "Ford" }];
let allCars = [nextCar, ...myCarsModels].flat();
console.log(allCars);
We now have an array of car record objects;
[ { id: 'recJKL010', name: 'Ford' },
{ id: 'recABC123', name: 'BMW' },
{ id: 'recDEF456', name: 'Volvo' },
{ id: 'recGHI789', name: 'Holden' } ]