Help

Scripting Block to add multiple linked records - Error

Topic Labels: Extensions
Solved
Jump to Solution
1752 2
cancel
Showing results for 
Search instead for 
Did you mean: 
jowan_qupty
6 - Interface Innovator
6 - Interface Innovator

Hi All,

I have a base that acts as a Property Management System with multiple tables. Two of those tables are the “Contracts” table and “Bills” Table. I have been using the new scripting block to streamline all of the daily processes: Recording new payments received and issuing receipts, creating customised reports of income/outstanding bills reports, by City/Property/Tenant reports …etc.

I am finally at the last step where i am attempting to create a script that prompts the user to insert all of the relevant contract information and creates a new contract record in my “Contracts” table, and then also automatically adds corresponding bills (in the Bills table) in the wanted frequency (i.e: 12 bills of monthly rental payments with due dates and amounts), and finally links them to the contract.

However, I am getting a strange behavior when adding the bills. The creation of the contract record works just fine but when the script attempts to add the bills I get the following error:

SyntaxError: await is only valid in async function
on line 1
at s on line 1
at Generator._invoke on line 1
at Generator.forEach.e.(anonymous function) [as next] on line 1
at e on line 1
at l on line 1
on line 1
on line 1

BELOW IS PART OF MY CODE:

    let contractsTable = base.getTable('📋Contracts');
    let unitsTable = base.getTable('🗝 Units');
    let billsTable = base.getTable('🧾 Bills');
    let tenantsTable = base.getTable('👥 Tenants');

////input and processing////

//adding contract
    let newContract = await contractsTable.createRecordAsync({
        'Unit Name': [{ 'id': unit.id }],
        'Tenants': [{ 'id': tenant.id }],
        //'Contract Attachment': [file],
        'Contract Signing Date': signingDate,
        'Lease Start': startDate,
        'Lease Expiration': endDate,
        'Master/Sublease': masterSub,
        'Type of Contract': contractType,
        'Base Period Monthly Rent': monthlyRent,
        'Currency': currency,
        'Payment Frequency': frequency,
        'Yearly Escalation': yearlyEscalation / 100,
        'Months of Grace Period': gracePeriod
    });

////some more processing////

//adding bills
for (var i = 0; i < fullBillAmount; i++) {
        let billDate = i == 0 ? startDate : new Date(startDate.setMonth(startDate.getMonth() + freq))
        let tempMonthlyBillAmount = rent * (Math.pow(1 + (esc / 100), Math.floor((i * freq) / 12)))
        var billAmount = 0;
        if (gracePeriod > 0) {
            if (gracePeriod > freq) {
                billAmount = 0;
                gracePeriod -= freq;
            }
            else {
                billAmount = (freq - gracePeriod) * tempMonthlyBillAmount;
                gracePeriod -= gracePeriod;
            }
        } else billAmount = freq * tempMonthlyBillAmount;
        billDate.setHours(0, 0, 0, 0)
        console.log('i: ', i, billDate, billAmount);
        await billsTable.createRecordAsync({
            'Contracts': [{ 'id': newContract }],
            'Type': billtType,
            'Due Date': billDate,
            'Bill Amount': rent * freq * (Math.pow(1 + (esc / 100), Math.floor((i * freq) / 12))),
            'Bill Currency': currency
        });
    }
    if (extraMonths > 0) {
        let billDate = new Date(startDate.setMonth(startDate.getMonth() + freq));
        let billAmount = rent * extraMonths * (Math.pow(1 + (esc / 100), Math.floor((fullBillAmount * freq) / 12)));
        billDate.setHours(0, 0, 0, 0)
        await billsTable.createRecordAsync({
            'Contracts': [{ 'id': newContract }],
            'Type': billtType,
            'Due Date': billDate,
            'Bill Amount': billAmount,
            'Bill Currency': currency
        });
        console.log(billDate, billAmount)
    }

if I remove the “await” from the last 2 “billsTable.createRecordAsync”, the program runs without error but does not add any bills to the table.

Strange thing is that I have a different script that does almost the same exact thing (adds one main record in one table and adds many records linked to that main record, in another table), but I don’t get this error.

Can anyone spot any obvious errors in my script? Any help would be extremely appreciated!
Thanks in advance!!

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Your error message says that await is only valid in async function. Instead of taking out the await, I recommend adding in async in front of the declaration for the function that contains the awaited statement.

However, I suspect that there is some other issue as well beyond the await/async issue. Notice that you are not keeping track of the return value of the createRecordsAsync call. You could try capturing the return value and see if it is an error message. For example, you might have a typo (should billtType be billType?) or an incorrect write format somewhere.

See Solution in Thread

2 Replies 2
kuovonne
18 - Pluto
18 - Pluto

Your error message says that await is only valid in async function. Instead of taking out the await, I recommend adding in async in front of the declaration for the function that contains the awaited statement.

However, I suspect that there is some other issue as well beyond the await/async issue. Notice that you are not keeping track of the return value of the createRecordsAsync call. You could try capturing the return value and see if it is an error message. For example, you might have a typo (should billtType be billType?) or an incorrect write format somewhere.

Thank you @kuovonne!
That was it. Works like a charm now :slightly_smiling_face: