Stock(Inventory Adjustment) sync in NetSuite to Shopify

Jira Code: DAZ-30

  • Inventory Adjustment can be triggered from NetSuite to Shopify.
  • A Userevent Script is deployed to initiate Inventory Adjustment in shopify.
  • When an Inventory adjustment is created, fetches the items count from the line items.
  • It then fetches the variant id of the corresponding item and calls API to adjust the inventory for this id in Shopify.
  • The count of inventory in NetSuite is added to the existing count in Shopify.
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * DAZ-22 UE Stock update in Shopify From Netsuite   
 * **************************************************************************
 * 
 * Date: 16-05-2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 *****************************************************************************
 **/
define(['N/format', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget'],

    function(format, url, search, runtime, record, https, serverWidget) {
        var ITEM_OBJ = [];
        var main = {
        		afterSubmit: function(scriptContext) {
                log.debug("scriptContext", scriptContext);
                var newRecId = scriptContext.newRecord.id;
                if (scriptContext.type == "create") {

                    var inventoryAdj = record.load({
                        type: "inventoryadjustment",
                        id: newRecId,
                        isDynamic: false
                    });
                    var numLines = inventoryAdj.getLineCount({
                        sublistId: 'inventory'
                    });
                    var inventorydetails = [];
                    for (var k = 0; k < numLines; k++) {
                        var itemid = inventoryAdj.getSublistValue({
                            sublistId: 'inventory',
                            fieldId: 'item',
                            line: k
                        });
                        var location = inventoryAdj.getSublistValue({
                            sublistId: 'inventory',
                            fieldId: 'location',
                            line: k
                        });
                        var adjustqty = inventoryAdj.getSublistValue({
                            sublistId: 'inventory',
                            fieldId: 'adjustqtyby',
                            line: k
                        });
                        var VARIANT_ID = main.getItemVariantID(itemid);
                        inventorydetails.push({
                            'Id': VARIANT_ID,
                            'qty': adjustqty
                        });

                    }
                    log.debug(inventorydetails);
                    if (inventorydetails.length != 0) {
                        for (var i = 0; i < inventorydetails.length; i++) {
                            var data = 0; //
                            var variaId = inventorydetails[i].Id;
                            log.debug('variaId', variaId);
                            var variaQty = inventorydetails[i].qty;
                            log.debug('variaQty', variaQty);
                            var itemId = 6651650867230
                            var IID = main.variant(variaId);
                            log.debug('IID', IID);
                            main.update(variaQty, IID);
                        }
                    }
                }

            },
            mappedItems: function(INTERNAL_ID) {
                var itemSearchObj = search.create({
                    type: "inventoryitem",
                    filters: [
                        ["internalid", "noneof", "@NONE@"], "AND", ["isinactive", "is", "F"]
                    ],
                    columns: [search.createColumn({ name: "externalid", label: "External ID" }), search.createColumn({ name: "internalid", label: "Internal ID" })]
                })
                var searchResultCount = itemSearchObj.runPaged().count;
                itemSearchObj.run().each(function(result) {
                    var externalID = result.getValue(itemSearchObj.columns[0]);
                    var internalID = result.getValue(itemSearchObj.columns[1]);
                    //if (externalID.includes('/'))
                    if (externalID.indexOf('/') != -1)
                        externalID = externalID.split('/')[1];
                    ITEM_OBJ[internalID] = externalID;
                    return true;
                });
                if (!ITEM_OBJ[INTERNAL_ID])
                    ITEM_OBJ[INTERNAL_ID] = false;
                return ITEM_OBJ[INTERNAL_ID];
            },
            getItemVariantID: function(INTERNAL_ID) {
                //if (ITEM_OBJ[VARIANT_ID] !== false)
                if (ITEM_OBJ[INTERNAL_ID])
                    return ITEM_OBJ[INTERNAL_ID];
                else {
                    return main.mappedItems(INTERNAL_ID);
                }
            },
            variant: function(data) {
                var variant_response = https.get({
                    url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/variants/' + data + '.json',
                    headers: {
                        "Accept": "application/json",
                        "Content-Type": "application/json"
                    }
                });
                var variant_content = JSON.parse(variant_response.body);
                variant_content = variant_content.variant.inventory_item_id
                log.debug("variant_content", variant_content);
                return variant_content;
            },
            update: function(data, itemId) {
                log.debug("itemId", itemId);

                var stock_response = https.post({
                    url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/inventory_levels/adjust.json',
                    headers: {
                        "Accept": "application/json",
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        "location_id": 20924424,
                        "inventory_item_id": itemId,
                        "available_adjustment": data
                    })
                });
                var stock_content = JSON.parse(stock_response.body);
                log.debug("stock_content", stock_content);
            }

        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        };

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;

    });

Leave a comment

Your email address will not be published. Required fields are marked *