Updated .....
function draftBills()
{
try
{
var data = JSON.parse(result.getContentText());
var bills = data.bills;
var body = "", count = 0, words = "", bill = [], poNums = [];
for (var i = 0; i <= bills.length-1; i++)
{
data = JSON.parse(result.getContentText());
bill = data.bill;
poNums = bill.purchaseorder_ids;
if (poNums.length < 1) continue;
for (var p = 0; p <= poNums.length-1; p++)
{
data = JSON.parse(result.getContentText());
if (data.purchaseorder.received_status != "received") continue
count++;
result = openDraftBills(bill.bill_id);
if (result === true) {words = ", <b>has been converted to open</b>"}
else words = result;
body = body + "<b>" + poNums[p] + "</b> is now received from " + bills[i].vendor_name + " - " + bills[i].bill_number + words + "<br>";
}
}
if (count > 0)
{
MailApp.sendEmail(
{
to: "delugeguys@zoho.com",
subject: count + " PO receives for draft bills",
htmlBody: body
})
}
} catch (e) {log_("An error occured in draftBills: " + e.message + " : line number = " + e.lineNumber)};
return;
}
function openDraftBills(iD)
{
try
{
var data = JSON.parse(result.getContentText());
var bill = data.bill;
if (bill.status != "draft") return " - Was already open!";
var vendor = bill.vendor_id;
var billArray =
{
"vendor_id": vendor,
"bill_number": iD,
"status": "open"
}
var newArray = encodeURIComponent(JSON.stringify(billArray));
var options = {
"method":"put",
"contentType":"application/json"
//'muteHttpExceptions' : true
}
var response = UrlFetchApp.fetch(url,options);
//var x = JSON.parse(response.getContentText());
return true;
} catch (e) {return " - Error converting bill to open: " + e.message + " : line number = " + e.lineNumber}
}