Home  >   FAQs   >  How to write a script for a server job to update stocks weekly in the warehouse system?
FAQs in this section

How to write a script for a server job to update stocks weekly in the warehouse system?

Table of Contents
Consider a company CompanyY, which sells construction materials. To manage their sales and billing they use CRM. To manage their warehouse they use a different warehouse management software.

Sometimes stocks available in the warehouse don't match with the data available in CRM. This happens because of human error but identifying that and correcting it takes a lot of time since they need to verify all the older data and find where the problem occurred.

To solve this problem, they want a weekly report of available stocks from CRM and warehouse software in a common tool where they can compare stocks for each product and find if there is any discrepancy. For any week if there is any discrepancy, they don’t need to verify historical data and verifying data for that week will be sufficient.

Their warehouse management system provides an API to update stocks available in other systems for comparison. From the CRM they need to send this data weekly to the warehouse system.

For that, you must create a weekly Server Job which runs every Sunday morning. It generates stock data of all available products and sends it to the warehouse system.
 
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) {
    
}
}
Was this FAQ helpful?
0  out of  0  found this helpful.
Comments 0
Be the first to comment
© Copyright 2023 Vtiger. All rights reserved.