Aug 11, 2023 05:57 AM
Hi everyone,
I'm trying to script a button to gather info from airtable and send it to an API which will turn it into an invoice and send it off. I'm getting an Error that makes no sense.
The code:
const customerNameField = 'Name';
const customerEmailField = 'Email';
const detailsField = 'Details';
const priceField = 'Price';
const amountField = 'Amount';
// Replace these values with your developer email and API key
const developerEmail = 'yehuda@officeours.co.il';
const apiKey = 'APIKEY';
// Replace this with the EZcount API endpoint
const ezcountApiEndpoint = 'https://demo.ezcount.co.il/api/createDoc';
// Get the current record's field values
const customerName = record.getCellValue(customerNameField);
const customerEmail = record.getCellValue(customerEmailField);
const itemDetails = record.getCellValue(detailsField);
const itemPrice = record.getCellValue(priceField);
const itemAmount = record.getCellValue(amountField);
// Construct the cc_email array
const ccEmails = [customerEmail, customerEmail]; // Use the same email twice
// Construct the JSON object
const jsonData = {
developer_email: developerEmail,
api_key: apiKey,
type: 320,
customer_name: customerName,
customer_email: customerEmail,
cc_email: ccEmails,
forceItems: 1,
show_items_including_vat: 1,
item: [
{
details: itemDetails,
price: itemPrice,
amount: itemAmount,
vat_type: 'INC' // You might need to adjust this
}
],
discount_value: 5,
discount_type: 'PERCENTAGE',
vat: '18',
price_total: 248,
tax_deducation: 10,
pay_until: '07/06/2020',
comment: 'Promise to fix all of your network issues',
email_text: 'Thank you for purecash from our shop.',
dont_send_email: 0,
send_copy: 1,
print_type: 'PDF',
auto_balance: 1
};
// Make an HTTP POST request to EZcount API
const response = await fetch(ezcountApiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
});
// Handle the response from the EZcount API
let responseMessage = '';
if (response.ok) {
responseMessage = await response.text();
output.markdown('Invoice created successfully:', responseMessage);
} else {
responseMessage = `Error creating invoice. Status: ${response.status}`;
output.markdown(responseMessage);
}
// Update a specific Airtable field with the response
await table.updateRecordAsync(record, {
'Response Field Name': Confirmation
});
on line 1 at s on line 1 at Generator._invoke on line 1 at Generator.next on line 1 at t on line 1 at a on line 1 on line 1 on line 1
Can anyone help?
Aug 11, 2023 06:06 AM
Sorry, I don’t know scripting so I can’t help you there. Somebody else will hopefully chime in below to help you with that. However, it is often easier to communicate with external APIs by either using DataFetcher or Make’s HTTP Module (assuming that Make doesn’t already have native support for the app).
Aug 11, 2023 07:52 AM
Thanks!
Data fetcher seems to only support outputting whole tables or views, which doesn't work for this.
Rather silly of me not to think of Make though.
Aug 11, 2023 02:27 PM - edited Aug 11, 2023 02:32 PM
There a few things I see;
Confirmation is not defined.
You're not selecting a record in the beginning.
I can make this run if I comment out the api call so anything else wrong with it would be associated with the API call. Also you may need to adjust the responseMessage output to suit your needs. Hope this helps.