Help

Re: Line Item Code Block

655 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Cameron_Mirza1
4 - Data Explorer
4 - Data Explorer

In the blog post about the new button feature, there is a use-case where a button is being used to add a new line item under “run a script”. See gif below:

run-script-add-line-item

If I open the Product Catalog template, I’m able to extract most of the code, however it is missing the part where it gives you the option to another line item or select “done”.

Is someone able to help me add that to the existing code below?

Thanks!

let lineItemsTable = base.getTable(‘Order line items’);
let clientOrdersTable = base.getTable(‘Client orders’);
let furnitureTable = base.getTable(‘Furniture’);

output.markdown(’## Add a line item’);

let clientOrdersRecord = await input.recordAsync(‘Pick a client order’, clientOrdersTable);
if (clientOrdersRecord) {
let furnitureRecord = await input.recordAsync(‘Pick a furniture item’, furnitureTable);
if (furnitureRecord) {
let quantity = parseInt(await input.textAsync(‘Enter the quantity’), 10);
let recordId = await lineItemsTable.createRecordAsync({
‘Belongs to order’: [{ id: clientOrdersRecord.id }],
‘Furniture item’: [{ id: furnitureRecord.id }],
‘Quantity’: quantity
});
let allRecords = await lineItemsTable.selectRecordsAsync();
let record = allRecords.records.find(record => record.id === recordId);
output.markdown(Added new line item: **${record.getCellValueAsString('Name')}**);
} else {
output.markdown(‘No item picked’);
}
} else {
output.markdown(‘No client picked’);
}

2 Replies 2

Not sure how it was originally done in that example, but here’s an option:

Inside if(clientOrdersRecord) {...} add something like:
let shouldContinue = true

Then place what you currently have in if(clientOrdersRecord) {} inside a loop:
while(shouldContinue == true) {...}

And at the bottom of that loop, still inside it, add something like:

shouldContinue = await input.buttonsAsync("Anything else?", [
    {label: "Add more", value: true},
    {label: "Done", value: false}
])
Todd_Zimmerman
5 - Automation Enthusiast
5 - Automation Enthusiast

For anyone else looking for this solution, I combined the code above with @Kamille_Parks suggestion and found it works. Here's my edited version (tables and fields changed for my own application):

let lineItemsTable = base.getTable('Order items');
let clientOrdersTable = base.getTable('Orders');
let productTable = base.getTable('Products');
  
output.markdown('## Add a line item');
  
let clientOrdersRecord = await input.recordAsync('Pick a client order', clientOrdersTable);
if (clientOrdersRecord) {
	let shouldContinue = true
	while(shouldContinue == true) {
    let productRecord = await input.recordAsync('Pick a product', productTable);
    if (productRecord) {
        let quantity = parseInt(await input.textAsync('Enter the quantity'), 10);
        let recordId = await lineItemsTable.createRecordAsync({
            'Order ID': [{ id: clientOrdersRecord.id }],
            'Product': [{ id: productRecord.id }],
            'Qty': quantity
        });
        let allRecords = await lineItemsTable.selectRecordsAsync();
        let record = allRecords.records.find(record => record.id === recordId);
        output.markdown(`Added new line item: **${record.getCellValueAsString('item ID')}**`);
    } else {
        output.markdown('No item picked');
    }
shouldContinue = await input.buttonsAsync("Anything else?", [
    {label: "Add more", value: true},
    {label: "Done", value: false}
])
} 
}
else {
    output.markdown('No client picked');
}

The only thing I'd like to add is to have a button pre-selected by default at each step, to reduce clicking/tabbing a bit. I haven't dug into this yet & would welcome suggestions.