Help

Re: Non-Error in scripting extension

1362 0
cancel
Showing results for 
Search instead for 
Did you mean: 
OfficeOurs
7 - App Architect
7 - App Architect

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
});

ERROR

SyntaxError: Unexpected token ')'
    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?
3 Replies 3

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).

 

 

 

 

 

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.

Ron_Williams
6 - Interface Innovator
6 - Interface Innovator

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.

 

 
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 table values
// Change this to match your base.
const table = base.getTable('Table 3');

// When run from a button field, the script skips the prompt
// and automatically uses the button's record.
const record = await input.recordAsync('Choose a record', table);
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
};
console.log(jsonData);

// 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();
responseMessage = `Invoice created successfully: ${responseMessage}`;
} else {
responseMessage = `Error creating invoice. Status: ${response.status}`;
}

// Update a specific Airtable field with the response
await table.updateRecordAsync(record {
'Response Field Name': responseMessage
});