async function main(record, user) { //Get organization id from invoice var orgId = record.account_id ? record.account_id.id : false; if(orgId) { try { //Fetch all related invoices for organization using rest api var relatedRes = await vtap.macro.ws.api('GET', 'retrieve_related', { id: '3x'+orgId, relatedLabel: 'Invoice', relatedType: 'Invoice' }); if(relatedRes && relatedRes.success) { var overdueAmount = 0; //For each related invoice, sum the balance amount for(var index in relatedRes.result) { var invoice = relatedRes.result[index]; var balance = parseFloat(invoice.balance); if(balance && !isNaN(balance)) { overdueAmount = overdueAmount + balance; } } //Round off the amount to 8 decimal digits overdueAmount = Math.round(overdueAmount * 100000000) / 100000000; //Update outstanding amount to organization using rest api revise method await vtap.macro.ws.revise({ id: '3x'+orgId, cf_outstanding_invoice_amount: overdueAmount }); } } catch(error) { } } } |