Loop through API call

165 views
Skip to first unread message

Michael Timpano

unread,
Nov 16, 2022, 10:50:55 PM11/16/22
to Google Apps Script Community
I am getting a  ReferenceError: getLastID is not defined error. I am trying to get the last item of the first array and pass that value into the url to get the subsequent arrays. I can get the last item from the first array, but I am not successful in passing it in the next loop.

Note. The lastID numbers are not incremental. Any suggestions or explanation would be helpful. Thank you.

function getTenderTransactionsCall() {

  const root = 'https://<url address>'
  const endpoint = 'tender_transactions.json?'
  const recordCount = 'limit=250&'
  const startDate = 'processed_at_min=2022-01-01+00:01:00+-0600&'
  const endDate = 'processed_at_max=2022-02-31+23:59:00+-0600'

  const url = root + endpoint + recordCount + startDate + endDate

  const firstBatch = callAPI(url, 0, params)
  console.log(firstBatch)

  pasteArrayIntoSheet(SPREADSHEET_ID, firstBatch, "Copy of tender transactions")
}


function callLastSinceID(url, lastNumber, params) {

  let idNumber = '&since_id=' + lastNumber

  while (lastNumber != -1) {
    let response = UrlFetchApp.fetch(url + idNumber, params)
    let responseCode = response.getResponseCode() // 200 response code OK  
    let data = JSON.parse(response.getContentText())

    let totalTenderTransactions = []

    if (responseCode === 200) {
      let results = data.tender_transactions
      //loop over the results
      results.forEach(row => {

        // get data elements of object
        let id = row.id
        let order_id = row.order_id 
        let amount = row.amount
        let date = row.processed_at
        let paymentMethod = row.payment_method

        //push data into an array
        totalTenderTransactions.push([id, order_id, amount, date, paymentMethod])

      })
      //get last ID item of the array
      let getlastID = totalTenderTransactions[totalTenderTransactions.length - 1][0]
    
    if (totalTenderTransactions.length < 249) {
      lastNumber = -1
    }
    else {
      lastNumber = getLastID
      console.log(lastNumber)
      let idNumber = '&since_id=' + lastNumber
      console.log(idNumber)

      return totalTenderTransactions
     }
   }
 }
}

cwl...@gmail.com

unread,
Nov 18, 2022, 8:10:57 AM11/18/22
to Google Apps Script Community
Without seeing the entire code, I have to make assumptions that there is nothing wrong in the other functions. 

Aside from the " pasteArrayIntoSheet(SPREADSHEET_ID, firstBatch, "Copy of tender transactions")"  function call, the rest is really just javascript. Have you been able to get it working outside of apps script? 

If you don't have something like VSCode and Node installed, you can still test your javascript function in the developer tools console (F12). Instead of Apps Scripts' "URLFetch", just use plain javascript (fetch). It may seem tedious, but that is how I usually create apps and get them working first before converting javascript over to specific apps script methods (e.g., fetch => Urlfetch).

Not sure if any of this helps, but don't limit yourself to just the Apps script IDE. :)

Michael Timpano

unread,
Nov 18, 2022, 1:04:20 PM11/18/22
to google-apps-sc...@googlegroups.com
@cwl.. do you have a recommendation on a site or resources where I can learn to
code with Node?  The other functions are working properly. The code doesn't complete the loop to get the next batches. Thanks for your response. I will look into VSCode and Node. Take care.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/nsQrSY5Hi2E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/eeb15091-5868-4826-80ab-fc2dccc1c619n%40googlegroups.com.

cwl...@gmail.com

unread,
Nov 19, 2022, 10:54:30 AM11/19/22
to Google Apps Script Community
For learning NodeJS, this guy is a great instructor. He has a lot of good content on his site regarding javascript in general and many different javascript technologies:
Net Ninja:  https://www.youtube.com/playlist?list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU

And for integrating Apps Script and Google products with NodeJS, VSCode, etc, these two channels are really good:

Get __it Done! :  https://www.youtube.com/channel/UCJebzvfCuA5ymtb79DQm36A

cwl...@gmail.com

unread,
Nov 19, 2022, 10:56:41 AM11/19/22
to Google Apps Script Community

Michael Timpano

unread,
Nov 20, 2022, 1:27:20 PM11/20/22
to google-apps-sc...@googlegroups.com
Tthanks for your response and the resource links.

Reply all
Reply to author
Forward
0 new messages