async function main() { try { //Get quantity in stock details for all active products using rest api query method var response = await vtap.macro.ws.query("SELECT productname, product_no, qtyinstock FROM Products WHERE discontinued = 1 AND qtyinstock > 0;"); if(response && response.success && response.result) { //Prepare stock data as required by warehouse management system var stockData = []; for(var index in response.result) { var productData = response.result[index]; stockData.push({ id: productData.product_no, name: productData.productname, quantity: parseInt(productData.qtyinstock) }); } //Generating current date in YYYY-MM-DD format as required by warehouse system var currentDateObj = new Date(); var currentDateVal = currentDateObj.getFullYear().toString() + '-' + (currentDateObj.getMonth() + 1).toString().padStart(2, '0') + '-' + currentDateObj.getDate().toString().padStart(2, '0'); //Send stock data to warehouse system using http post method await vtap.macro.http.post( 'https://company.warehouse.com/api/externalstock', { headers: { Authorization: 'Basic XXXXXXXXXX' }, body: { source: 'CRM', date: currentDateVal, stocks: stockData }, json: true }); } } catch(error) { } } |