Apr 22, 2022 04:17 AM
Could anyone advise on how to do this? I have a “Quote” record which is composed of its linked “Line Item” records.
In order to create a new version of a Quote I’m trying to create copy of the Quote, copies of all the Quote’s Line Items and set the new Line Items as the value for the new Quote’s Line Item field.
The problem is that I can never get the Line Item field to accept my new Line Items ids. I’m sure this has to do with the order of async
and await
operations but I can’t figure it out. My latest attempt follows;
const lineItems = useRecords(lineItemsTable);
async function createLineItemObject(fields) {
const newLineItemId = await lineItemsTable.createRecordAsync(fields);
return { id: newLineItemId };
}
async function createQuote(fields) {
const newQuoteId = await quotesTable.createRecordAsync(fields);
return newQuoteId;
}
async function handleNewVersion(clickedQuote) {
const matchingLineItems = lineItems.filter((lineItem) => {
const quotesCell = lineItem.getCellValue("Quote");
return quotesCell ? quotesCell[0].id === clickedQuote.id : null;
});
const newLineItems = matchingLineItems.map((item) => {
return createLineItemObject({
Item: item.getCellValue("Item"),
Purchase: item.getCellValue("Purchase"),
"Price Per Unit Per Week": item.getCellValue("Price Per Unit Per Week"),
"Risk Value": item.getCellValue("Risk Value"),
"Item Quantity": item.getCellValue("Item Quantity"),
"Hire End": item.getCellValue("Hire End"),
"Quote Index": item.getCellValue("Quote Index"),
});
});
const newQuoteId = createQuote({
Status: { name: "Draft" },
Job: [{ id: job.id }],
Notes: clickedQuote.getCellValue("Notes"),
"Line Items": newLineItems,
"Hire Start": clickedQuote.getCellValue("Hire Start"),
"Hire Duration": clickedQuote.getCellValue("Hire Duration"),
"Delivery Address": clickedQuote.getCellValue("Delivery Address"),
"Delivery Address 2": clickedQuote.getCellValue("Delivery Address 2"),
"Delivery City": clickedQuote.getCellValue("Delivery City"),
"Delivery Postcode": clickedQuote.getCellValue("Delivery Postcode"),
"Collection Address": clickedQuote.getCellValue("Collection Address"),
"Collection Address 2": clickedQuote.getCellValue("Collection Address 2"),
"Collection City": clickedQuote.getCellValue("Collection City"),
"Collection Postcode": clickedQuote.getCellValue("Collection Postcode"),
"Delivery Price": clickedQuote.getCellValue("Delivery Price"),
"Collection Price": clickedQuote.getCellValue("Collection Price"),
});
}
Apr 22, 2022 05:17 AM
Believe I’ve figured it out, if anyone spots a big problem with this I’d appreciate a shout! But it’s working now;
const lineItems = useRecords(lineItemsTable);
async function handleNewVersion(clickedQuote) {
async function createLineItem(fields) {
const newLineItemId = await lineItemsTable.createRecordAsync(fields);
return newLineItemId;
}
const matchingLineItems = lineItems.filter((lineItem) => {
const quotesCell = lineItem.getCellValue("Quote");
return quotesCell ? quotesCell[0].id === clickedQuote.id : null;
});
let newLineItemIds = [];
for (let item of matchingLineItems) {
const newLineItemId = await createLineItem({
Item: item.getCellValue("Item"),
Purchase: item.getCellValue("Purchase"),
"Price Per Unit Per Week": item.getCellValue("Price Per Unit Per Week"),
"Risk Value": item.getCellValue("Risk Value"),
"Item Quantity": item.getCellValue("Item Quantity"),
"Hire End": item.getCellValue("Hire End"),
"Quote Index": item.getCellValue("Quote Index"),
});
newLineItemIds.push(newLineItemId);
}
quotesTable.createRecordAsync({
Status: { name: "Draft" },
Job: [{ id: job.id }],
Notes: clickedQuote.getCellValue("Notes"),
"Line Items": newLineItemIds.map((item) => {
return { id: item };
}),
"Hire Start": clickedQuote.getCellValue("Hire Start"),
"Hire Duration": clickedQuote.getCellValue("Hire Duration"),
"Delivery Address": clickedQuote.getCellValue("Delivery Address"),
"Delivery Address 2": clickedQuote.getCellValue("Delivery Address 2"),
"Delivery City": clickedQuote.getCellValue("Delivery City"),
"Delivery Postcode": clickedQuote.getCellValue("Delivery Postcode"),
"Collection Address": clickedQuote.getCellValue("Collection Address"),
"Collection Address 2": clickedQuote.getCellValue("Collection Address 2"),
"Collection City": clickedQuote.getCellValue("Collection City"),
"Collection Postcode": clickedQuote.getCellValue("Collection Postcode"),
"Delivery Price": clickedQuote.getCellValue("Delivery Price"),
"Collection Price": clickedQuote.getCellValue("Collection Price"),
});
}
Apr 22, 2022 09:38 AM
This seems inefficient. You could batch create the records using lineItemsTable.createRecordsAsync()
. Just like its singular function, the batch function returns the IDs of the new records, so you don’t need to push each ID to an array; it already returns an array.