Update Item Base Price NetSuite to Big Commerce

Below code defines if the base price is updated for an item in NetSuite, the same will get updated in Big Commerce.

Script: User event


define(['N/record', 'N/search', 'N/https', '../NUTC 40 Big Commerce Common Library/jj_big_commerce_common_library_nutc40.js'],
    /**
   * @param{record} record
   * @param{search} search
   * @param{https} https
   * @param{LibraryScript} bigComLibrary
   */
    (record, search, https, bigComLibrary) => {

        /**
 * @function afterSubmit
 * @param{Object} context
*/
        const afterSubmit = (scriptContext) => {
            let prodResponse;
            const itemRecord = scriptContext.newRecord;
            const itemId = itemRecord.id;
            const sku = itemRecord.getValue({
                fieldId: 'itemid'
            })
            try {

                // const productAPI = '/v3/catalog/products?keyword=' + sku; - Live API

                const productAPI = '/v3/catalog/products?sku=JJTestItem';
                const response = bigComLibrary.commonFunctions.sendGetAPIRequest(productAPI);
                const getProduct = JSON.parse(response.body);
                const productId = getProduct.data[0].id;
                const productName = getProduct.data[0].name;
                const productType = getProduct.data[0].type;
                log.debug("Product Data", productId)
                let itemPrice = [];

                const pricingSearch = search.create({
                    type: 'item',
                    filters: [
                        ['internalid', 'is', itemId], 'and',
                        ['pricing.pricelevel', 'is', 1]
                    ],
                    columns: [
                        search.createColumn({ name: "maximumquantity", join: 'pricing', label: "Maximum Quantity" }),
                        search.createColumn({ name: "minimumquantity", join: 'pricing', label: "Minimum Qunatity", sort: "ASC" }),
                        search.createColumn({ name: "unitprice", join: 'pricing', label: "Base Price" }),
                    ]
                });
                pricingSearch.run().each(function (result) {
                    itemPrice.push({
                        baseprice: Number(result.getValue({
                            name: "unitprice", join: 'pricing', label: "Base Price",
                        })),
                        maximumquantity: Number(result.getValue({ name: "maximumquantity", join: 'pricing', label: "Maximum Quantity" })),
                        minimumquantity: Number(result.getValue({ name: "minimumquantity", join: 'pricing', label: "Minimum Qunatity" })),
                        qtyprcshedule: result.getValue({ name: "minimumquantity", join: 'pricing', label: "Minimum Qunatity" }),
                    });
                    return true;
                })
                log.debug("Item Price", itemPrice)
              

                let basePrice = Number(itemPrice[0].baseprice);
                let postProductAPI = '/v3/catalog/products/' + productId
                let basePriceBody = {
                    "name": productName,
                    "type": productType,
                    "price": basePrice
                }
                prodResponse = postAPI(postProductAPI, basePriceBody)
                log.debug("Price Update", prodResponse)
            }
            catch (e) {
                log.error("Error", e.message)
                let cusErrRecord = record.create({
                    type: 'customrecord_jj_cr_itmpr_er_bgcom_nutc47',
                })
                cusErrRecord.setValue({
                    fieldId: 'custrecord_jj_err_details',
                    value: e
                });
                cusErrRecord.setValue({
                    fieldId: '	custrecord_jj_item_name',
                    value: sku
                });
                cusErrRecord.setValue({
                    fieldId: 'custrecord_jj_api_response',
                    value: prodResponse
                });
                let errRecordId = cusErrRecord.save({ ignoreMandatoryFields: true });
                log.error("Error Record ID", errRecordId);
            }
        }
        function putAPI(customURL, body) {
            try {
                let apiCredentials = bigComLibrary.commonFunctions.apiConfigFileSearch();
                let bigComURL = apiCredentials.urlPath + customURL;
                let response = https.put({
                    url: bigComURL,
                    headers: apiCredentials.headerObj,
                    body: JSON.stringify(body)
                });
                return response;
            } catch (e) {
                log.error("error in library module @ PostAPI", e);
                return false;
            }
        }
        function postAPI(customURL, body) {
            try {
                let apiCredentials = bigComLibrary.commonFunctions.apiConfigFileSearch();
                let bigComURL = apiCredentials.urlPath + customURL;
                let response = https.post({
                    url: bigComURL,
                    headers: apiCredentials.headerObj,
                    body: JSON.stringify(body)
                });
                return response;
            } catch (e) {
                log.error("error in library module @ PostAPI", e);
                return false;
            }
        }

        return { afterSubmit }

    });

Below functions defines to sent get requests to big commerce

apiConfigFileSearch()
            {
                let apiData = {}
                try {
                    let apiConfig = search.lookupFields({
                        type: "customrecord_jj_cr_bg_com_api_config_42",
                        id: 1,
                        columns: ['custrecord_jj_host_name', 'custrecord_jj_big_com_access_token']
                    });
                    apiData.urlPath = apiConfig.custrecord_jj_host_name;
                    let bearerToken = apiConfig.custrecord_jj_big_com_access_token;
                    let headerObj ={
                        'X-Auth-Token': bearerToken,
                        'Content-Type' : 'application/json',
                        'Accept': 'application/json',
                    };
                    apiData.headerObj = headerObj;
                } catch (e) {
                    log.error(' error in library module @ apiConfigFileSearch', e);
                    return false;
                }
                return apiData;
            },
            /**
             * @desc sends a get request to bigCom
             * @param customURL - end point to be used
             * @return JSON Object JSON body returned from API end point

             */
            sendGetAPIRequest(customURL) {
                try {
                    let apiCredentials = commonFunctions.apiConfigFileSearch();
                    let bigComURL = apiCredentials.urlPath + customURL;
                    let response = https.get({
                        url: bigComURL,
                        headers: apiCredentials.headerObj
                    });
                    return response;
                } catch (e) {
                    log.error("error in library module @ sendGetAPIRequest", e);
                    return false;
                }
            }

Leave a comment

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