Hi all
I’m really struggling with this - where is @Bill.French when you need him :-), Bill I need you! (or anyone else that can help please).
So I have a view based on my orders table that filters unfulfilled orders
I have written some code (with the help of others) that loops through the unfulfilled orders and creates new records in the Prod Req (Producction Required table).
This table contains the product sku as the primary field and has a Linked Orders field (linked record field) that contains all unfulfilled orders with their order numbers along with some rollup fields.
This code works well but it will create duplicate records in the Prod Req table each time it runs.
Ideally, I’d like the code to first compare the Unfulfilled Orders against the Prod Req table. If it finds the same sku in there, then add the associated order number/order record.id to the Linked Orders field. If the sku does not exist in the Prod Req table, then add it and also the linked orders.
Here’s the code…
1let orders = base.getTable("Daily Orders");23let pr = base.getTable("Prod Req");45let view = orders.getView("Unfulfilled Daily Orders");67let sku = orders.getField("Line Items: Sku"); 89let ordernum = orders.getField("Order: Name"); 1011let currsku = "";1213let ordstr = "";1415let rid = "";1617let Orderresult = await view.selectRecordsAsync({1819 sorts: [{field: sku, direction: "asc"}]2021});2223output.text("Source: Unfulfilled Daily Orders results array");2425output.inspect(Orderresult);2627let ordersArray = [];2829for (let record of Orderresult.records) {3031 3233 rid = record.id;3435 ordernum = record.getCellValue("Order: Name");3637 //output.text(ordernum);3839 currsku = record.getCellValue(sku)[0]; 4041 ordersArray.push({4243 fields: {4445 "id": rid,4647 "SKU": currsku4849 }5051 })5253}5455output.text("Unfulfilled Daily Orders results array by Record ID and SKU");5657output.inspect(ordersArray);5859let Prodresult = await pr.selectRecordsAsync();6061output.text("Source: Prod Req results array");6263output.inspect(Prodresult);6465let prodArray = [];6667for (let record of Prodresult.records) {6869 7071 rid = record.id;7273 currsku = record.getCellValue("sku"); 7475 prodArray.push({7677 fields: {7879 "rid": rid,8081 "SKU": currsku8283 }8485 }) 8687 8889}9091output.text("Prod Req results array by Record ID and SKU");9293output.inspect(prodArray);9495const skus = [...new Set(ordersArray.map(obj => obj.fields.SKU))];9697const linkedRecordsArray = skus.map(sku => {9899 return {100101 fields: {102103 //'id': rid,104105 'sku': sku,106107 'Linked Orders': ordersArray.filter(obj => obj.fields.SKU === sku).map(obj => ({ id: obj.fields.id }))108109 }110111 };112113});114115output.text("Prod Req records needed to be created array");116117output.inspect(linkedRecordsArray);118119output.text("This works fine but it will clearly duplicate records each time it runs");120121output.text("Ideally, I'd like the code to first compare the Unfulfilled Orders agsinst the Prod Req table. If it finds the same sku in there, then add the associaed order number/order record.id to the Linked Orders field.");122123output.text("If the sku does not exist in the Prod Req table, then add it and the linbked orders");124125let recordsCreated = await batchAnd('Create', pr, linkedRecordsArray);126127/*128129 Use this function to perform 'Update', 'Create', or 'Delete'130131 async actions on batches of records that could potentially 132133 more than 50 records.134135 ::PARAMETERS::136137 action = string; one of 3 values:138139 - 'Update' to call table.updateRecordsAsync()140141 - 'Create' to call table.createRecordsAsync()142143 - 'Delete' to call table.deleteRecordsAsync()144145 table = Table; the table the action will be performed in146147 records = Array; the records to perform the action on148149 - Ensure the record objects inside the array are150151 formatted properly for the action you wish to152153 perform154155 ::RETURNS::156157 recordsActedOn = integer, array of recordId's, or null; 158159 - Update Success: integer; the number of records processed by the function160161 - Delete Success: integer; the number of records processed by the function162163 - Create Success: array; the id strings of records created by the function164165 - Failure: null;166167*/168169async function batchAnd(action, table, records) {170171 let recordsActedOn;172173 switch (action) {174175 case 'Update':176177 recordsActedOn = records.length;178179 while (records.length > 0) {180181 await table.updateRecordsAsync(records.slice(0, 50));182183 records = records.slice(50);184185 };186187 break;188189 190191 case 'Create':192193 recordsActedOn = [];194195 while (records.length > 0) {196197 let recordIds = await table.createRecordsAsync(records.slice(0, 50));198199 recordsActedOn.push(...recordIds)200201 records = records.slice(50);202203 };204205 break;206207 case 'Delete':208209 recordsActedOn = records.length;210211 while (records.length > 0) {212213 await table.deleteRecordsAsync(records.slice(0, 50));214215 records = records.slice(50);216217 }218219 break;220221 default:222223 output.markdown(`**Please use either 'Update', 'Create', or 'Delete' as the "action" parameter for the "batchAnd()" function.**`);224225 recordsActedOn = null;226227 }228229 return recordsActedOn;230231}232
Thanks so much for your help. @Bill.French if you’re listening, I’d love your input please. If any answers can be as detailed as possible with code examples relevant to my tables / views it would really help as I’m only learning.
I’ve never coded in my life!!!
Thanks
Claire
Founder of Make Give Live
makegivelive.co.nz