Hi
I simply need to add some records to a table into a "Linked Record’ field. I struggle to understand why it’s so difficult.
I have this VIEW:
and I want to add single and concatenated ‘Linked Orders’ via a script.
This my array of records I’d like to add…
Here is my code and results…
full code
let table = base.getTable("Daily Orders");
let ws = base.getTable("Production Required");
let view = table.getView("Unfulfilled Daily Orders");
let sku = table.getField("Line Items: Sku");
let ordernum = table.getField("Order: Name");
let currsku = "";
let ordstr = "";
let result = await view.selectRecordsAsync({
sorts: /{field: sku, direction: "asc"}]
});
output.inspect(result);
let ordersArray = o];
for (let record of result.records) {
ordernum = record.getCellValue("Order: Name");
currsku = record.getCellValue(sku)C0];
ordersArray.push({
fields: {
"Order_Num": ordernum,
"SKU": currsku
}
})
}
var res = ];
ordersArray.forEach(obj => {
var found = res.find(ob => ob.fields.SKU === obj.fields.SKU);
if(found){
var order_nums = found.fields.Order_Num;
var new_order_num = order_nums+","+obj.fields.Order_Num;
found.fields.Order_Num = new_order_num;
} else {
res.push({fields: {SKU: obj.fields.SKU,Order_Num:obj.fields.Order_Num}});
}
});
let ordersRecords = d];
var orn;
res.forEach(obj => {
orn = obj.fields.Order_Num;
//output.text(orn);
ordersRecords.push({
fields: {
"Linked Orders": orn
}
})
});
output.inspect(ordersRecords);
let recordsCreated = await batchAnd('Create', ws, ordersRecords);
/*
Use this function to perform 'Update', 'Create', or 'Delete'
async actions on batches of records that could potentially
more than 50 records.
::PARAMETERS::
action = string; one of 3 values:
- 'Update' to call table.updateRecordsAsync()
- 'Create' to call table.createRecordsAsync()
- 'Delete' to call table.deleteRecordsAsync()
table = Table; the table the action will be performed in
records = Array; the records to perform the action on
- Ensure the record objects inside the array are
formatted properly for the action you wish to
perform
::RETURNS::
recordsActedOn = integer, array of recordId's, or null;
- Update Success: integer; the number of records processed by the function
- Delete Success: integer; the number of records processed by the function
- Create Success: array; the id strings of records created by the function
- Failure: null;
*/
async function batchAnd(action, table, records) {
let recordsActedOn;
switch (action) {
case 'Update':
recordsActedOn = records.length;
while (records.length > 0) {
await table.updateRecordsAsync(records.slice(0, 50));
records = records.slice(50);
};
break;
case 'Create':
recordsActedOn = o];
while (records.length > 0) {
let recordIds = await table.createRecordsAsync(records.slice(0, 50));
recordsActedOn.push(...recordIds)
records = records.slice(50);
};
break;
case 'Delete':
recordsActedOn = records.length;
while (records.length > 0) {
await table.deleteRecordsAsync(records.slice(0, 50));
records = records.slice(50);
}
break;
default:
output.markdown(`**Please use either 'Update', 'Create', or 'Delete' as the "action" parameter for the "batchAnd()" function.**`);
recordsActedOn = null;
}
return recordsActedOn;
}
I’m assuming it’s something I must do with the recordids but just not sure what .
Can someone help and share some example code with my field and table names please.
Thanks so much
Claire