Haydn Lillywhite
Haydn Lillywhite

Reputation: 11

Google Apps Script

I've created a simple script that reads through an xml file and posts the results to an SQL database. This works perfectly.

I've put a little if statement in the script to identify orders that have already been posted to SQL. Basically if the transactionID in the input array is higher than the highest transactionID on the SQL server it adds the row values to the output array.

It seems that I am missing a trick here because I am getting "TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")" when trying to compare the current xml row to the last transaction ID.

I've done some searching and whilst I can see people with similar problems the explanations don't make a whole lot of sense to me.

Anyway, here is the relevant part of the code. Note that this all works perfectly without the if() bit.

function getXML() {
  var id = lastTransactionID();
  var xmlSite = UrlFetchApp.fetch("https://api.eveonline.com/corp/WalletTransactions.xml.aspx?KeyID=1111&vCode=1111&accountKey=1001").getContentText();
  var xmlDoc = XmlService.parse(xmlSite);

  var root = xmlDoc.getRootElement();
  var row = new Array();

  row = root.getChild("result").getChild("rowset").getChildren("row");

  var output = new Array();

  var i = 0;
  for (j=0;i<row.length;j++){
    if(row[j].getAttribute("transactionID").getValue()>id){ //Produces: TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")
      output[i] = new Array();
      output[i][0] = row[j].getAttribute("transactionDateTime").getValue();
      output[i][1] = row[j].getAttribute("transactionID").getValue();
      output[i][2] = row[j].getAttribute("quantity").getValue();
      output[i][3] = row[j].getAttribute("typeName").getValue();
      output[i][4] = row[j].getAttribute("typeID").getValue();
      output[i][5] = row[j].getAttribute("price").getValue();
      output[i][6] = row[j].getAttribute("clientID").getValue();
      output[i][7] = row[j].getAttribute("clientName").getValue();
      output[i][8] = row[j].getAttribute("stationID").getValue();
      output[i][9] = row[j].getAttribute("stationName").getValue();
      output[i][10] = row[j].getAttribute("transactionType").getValue();
      output[i][11] = row[j].getAttribute("transactionFor").getValue();
      output[i][12] = row[j].getAttribute("journalTransactionID").getValue();
      output[i][13] = row[j].getAttribute("clientTypeID").getValue();
      i++;
    }
  }
  insert(output,output.length);
}

Upvotes: 0

Views: 425

Answers (1)

Haydn Lillywhite
Haydn Lillywhite

Reputation: 11

I have seen my mistake and corrected.

Mistake was in the for loop.

for (j=0;i

Upvotes: 1

Related Questions