Hi Team
I have the following script which works, but it’s give back limited data. I would like to see all the Inbound shipments what we have where the Quantity Ordered is NOT the same as Quantity Received (so basically Inbound).
If I expand the script then it fails or I reach my credit limit.
May I ask someones help how I can get all the “Open” Purchase orders out from the system?
Thanks!
Adam
my script:
function fetchPOsWithPendingItems() {
const token = getAuthToken();
const query = query { purchase_orders { data(last: 10) { edges { node { po_number created_at line_items { edges { node { sku quantity quantity_received } } } } } } } }
;
const response = UrlFetchApp.fetch(GRAPHQL_ENDPOINT, {
method: ‘post’,
contentType: ‘application/json’,
headers: {
‘Authorization’: 'Bearer ’ + token,
‘auth0-forwarded-for’: ‘127.0.0.1’,
},
payload: JSON.stringify({ query: query }),
});
const json = JSON.parse(response.getContentText());
if (!json.data || !json.data.purchase_orders || !json.data.purchase_orders.data) {
throw new Error(‘API returned no purchase orders data.’);
}
const purchaseOrders = json.data.purchase_orders.data.edges;
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(‘SHIPHERO_OPEN_POs’);
if (!sheet) throw new Error(‘Sheet “SHIPHERO_OPEN_POs” not found.’);
sheet.clear();
sheet.appendRow([‘PO Number’, ‘Created At’, ‘SKU’, ‘Quantity Ordered’, ‘Quantity Received’]);
purchaseOrders.forEach(po => {
const poNode = po.node;
const poNumber = poNode.po_number;
const createdAt = poNode.created_at;
poNode.line_items.edges.forEach(item => {
const itemNode = item.node;
const sku = itemNode.sku || 'N/A';
const quantityOrdered = itemNode.quantity || 0;
const quantityReceived = itemNode.quantity_received || 0;
if (quantityOrdered !== quantityReceived) {
sheet.appendRow([poNumber, createdAt, sku, quantityOrdered, quantityReceived]);
}
});
});
}