Restlet to create Non-Inventory Item

Jira Code: PG-1

Restlet that would accept JSON data and use it to create or update a non-inventory item.

The restlet should return an informative error code when not successful and should return the product information and internal id when available.

The restlet should let a caller using an HTTP GET to return information about a non-inventory item by supplying the items name.

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
/**
 * Script Description
 * Script to create a customer on customer registration in Magento
 **/
/*******************************************************************************
 * Prodigi
 * **************************************************************************
 * 
 * Date: 13-08-2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY 
 * 
 * Revision 1 $ 13-08-2019 anju.m : Created
 * 
 *****************************************************************************
 **/
define(['N/format', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/email'],
    /**
     * @param {format} format
     * @param {http} http
     * @param {https} https
     * @param {record} record
     * @param {runtime} runtime
     * @param {search} search
     */
    function (format, http, https, record, runtime, search, email) {

        /**
         * Function called upon sending a GET request to the RESTlet.
         *
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
         * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
         * @since 2015.1
         */
        function doGet(requestParams) {
            try {
                log.debug("requestParams", requestParams);
                var itemDetails = JSON.stringify(requestParams)
                itemDetails = JSON.parse(itemDetails)
                var itemRec;
                var itemId;
                var dataReturn = {};
                var noninventoryitemSearchObj = search.create({
                    type: "noninventoryitem",
                    filters:
                        [
                            ["type", "anyof", "NonInvtPart"],
                            "AND",
                            ["name", "is", itemDetails.sku]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = noninventoryitemSearchObj.runPaged().count;
                log.debug("noninventoryitemSearchObj result count", searchResultCount);
                if (searchResultCount > 0) {
                    noninventoryitemSearchObj.run().each(function (result) {
                        itemId = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        itemRec = record.load({
                            type: "noninventoryitem",
                            id: itemId,
                        });
                        dataReturn.sku = itemRec.getValue("itemid")
                        dataReturn.category = itemRec.getText("custitem_nbs_category")
                        dataReturn.productType = itemRec.getText("custitem_nbs_producttype")
                        dataReturn.horizontalDimension = itemRec.getText("custitem_nbs_horizontaldimension")
                        dataReturn.verticalDimension = itemRec.getText("custitem_nbs_verticaldimension")
                        dataReturn.dimensionUnit = itemRec.getText("custitem_nbs_dimensionunit")
                        dataReturn.frameColour = itemRec.getText("custitem_nbs_framecolour")
                        dataReturn.glaze = itemRec.getText("custitem_nbs_glaze")
                        dataReturn.frame = itemRec.getText("custitem_nbs_frame")
                        dataReturn.paperType = itemRec.getText("custitem_nbs_papertype")
                        dataReturn.mount = itemRec.getText("custitem_nbs_mount")
                        dataReturn.finish = itemRec.getText("custitem_nbs_finish")
                        dataReturn.edge = itemRec.getText("custitem_nbs_edge")
                        dataReturn.style = itemRec.getText("custitem_nbs_style")
                        dataReturn.gender = itemRec.getText("custitem_nbs_gender")
                        dataReturn.mountColour = itemRec.getText("custitem_nbs_mountcolour")
                        dataReturn.fulfilmentPartner = itemRec.getText("custitem_cseg_nbs_fufilmentp")
                        dataReturn.size = itemRec.getText("custitem2")
                        dataReturn.brand = itemRec.getText("custitem3")
                        dataReturn.wrap = itemRec.getText("custitem4")
                        dataReturn.apiVersion = itemRec.getText("custitem6")
                        dataReturn.quality = itemRec.getText("custitem5")
                        dataReturn.description = itemRec.getValue("salesdescription")
                    });
                    return dataReturn;
                }
            } catch (er) {
                log.debug("error @ get", er)
                return er.message
            }
        }

        /**
         * Function called upon sending a PUT request to the RESTlet.
         *
         * @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
         * or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
         * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
         * @since 2015.2
         */
        function doPut(requestBody) {

        }

        /**
         * Function called upon sending a POST request to the RESTlet.
         *
         * @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
         * or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
         * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
         * @since 2015.2
         */
        function doPost(requestBody) {
            try {
                log.debug("requestBody", requestBody);
                var itemDetails = JSON.stringify(requestBody)
                itemDetails = JSON.parse(itemDetails)
                var itemId;
                var itemCreated;
                var noninventoryitemSearchObj = search.create({
                    type: "noninventoryitem",
                    filters:
                        [
                            ["type", "anyof", "NonInvtPart"],
                            "AND",
                            ["name", "is", itemDetails.sku]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = noninventoryitemSearchObj.runPaged().count;
                log.debug("noninventoryitemSearchObj result count", searchResultCount);
                if (searchResultCount > 0) {
                    noninventoryitemSearchObj.run().each(function (result) {
                        itemId = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        var itemRec = record.load({
                            type: "noninventoryitem",
                            id: itemId,
                        });
                        itemCreated = updateValue(itemRec, itemDetails)
                    });
                } else {
                    var itemRec = record.create({
                        type: "noninventoryitem"
                    });
                    itemRec.setValue({
                        fieldId: 'itemid',
                        value: itemDetails.sku
                    });
                    itemCreated = updateValue(itemRec, itemDetails)
                }
                return 'Internal id of item Created/Updated is ' + itemCreated;
            }
            catch (e) {
                log.debug("error@", e);
                email.send({
                    author: -5,
                    recipients: 'tom@prodigi.com',
                    subject: "ERROR IN NON-INVENTORY ITEM CREATION",
                    body: e.message
                });
                return e.message;
            }
        }
        function updateValue(itemRec, itemDetails) {
            try {
                itemRec.setValue({
                    fieldId: 'subsidiary',
                    value: 2
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_category',
                    text: itemDetails.category
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_producttype',
                    text: itemDetails.productType
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_horizontaldimension',
                    text: itemDetails.horizontalDimension
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_verticaldimension',
                    text: itemDetails.verticalDimension
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_dimensionunit',
                    text: itemDetails.dimensionUnit
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_framecolour',
                    text: itemDetails.frameColour
                });
                var paperTypeSearch = search.create({
                    type: "customlist142",
                    filters:
                        [
                            ["name", "is", itemDetails.paperType]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = paperTypeSearch.runPaged().count;
                if (searchResultCount > 0) {
                    paperTypeSearch.run().each(function (result) {
                        var papertypeValue = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        itemRec.setValue({
                            fieldId: 'custitem_nbs_papertype',
                            value: papertypeValue
                        });
                    });
                }

                var glazeSearch = search.create({
                    type: "customlist147",
                    filters:
                        [
                            ["name", "is", itemDetails.glaze]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = glazeSearch.runPaged().count;
                if (searchResultCount > 0) {
                    glazeSearch.run().each(function (result) {
                        var glazeValue = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        itemRec.setValue({
                            fieldId: 'custitem_nbs_glaze',
                            value: glazeValue
                        });
                    });
                }

                var frameSearch = search.create({
                    type: "customlist141",
                    filters:
                        [
                            ["name", "is", itemDetails.frame]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = frameSearch.runPaged().count;
                if (searchResultCount > 0) {
                    frameSearch.run().each(function (result) {
                        var frameValue = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        itemRec.setValue({
                            fieldId: 'custitem_nbs_frame',
                            value: frameValue
                        });
                    });
                }
                var mountSearch = search.create({
                    type: "customlist148",
                    filters:
                        [
                            ["name", "is", itemDetails.mount]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = mountSearch.runPaged().count;
                if (searchResultCount > 0) {
                    mountSearch.run().each(function (result) {
                        var mountValue = result.getValue({
                            name: "internalid",
                            label: "Internal ID"
                        });
                        itemRec.setValue({
                            fieldId: 'custitem_nbs_mount',
                            value: mountValue
                        });
                    });
                }
                if (itemDetails.priceCurrency == "GBP") {
                    itemRec.setSublistValue({
                        sublistId: 'price1',
                        fieldId: 'price_1_',
                        line: 0,
                        value: parseFloat(itemDetails.price)
                    });
                } else if (itemDetails.priceCurrency == "US Dollar") {
                    itemRec.setSublistValue({
                        sublistId: 'price2',
                        fieldId: 'price_1_',
                        line: 0,
                        value: parseFloat(itemDetails.price)
                    });
                } else if (itemDetails.priceCurrency == "Australian Dollar") {
                    itemRec.setSublistValue({
                        sublistId: 'price5',
                        fieldId: 'price_1_',
                        line: 0,
                        value: parseFloat(itemDetails.price)
                    });
                } else if (itemDetails.priceCurrency == "Canadian Dollar") {
                    itemRec.setSublistValue({
                        sublistId: 'price3',
                        fieldId: 'price_1_',
                        line: 0,
                        value: parseFloat(itemDetails.price)
                    });
                } else if (itemDetails.priceCurrency == "Euro") {
                    itemRec.setSublistValue({
                        sublistId: 'price4',
                        fieldId: 'price_1_',
                        line: 0,
                        value: parseFloat(itemDetails.price)
                    });
                }
                itemRec.setText({
                    fieldId: 'custitem_nbs_finish',
                    text: itemDetails.finish
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_edge',
                    text: itemDetails.edge
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_style',
                    text: itemDetails.style
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_gender',
                    text: itemDetails.gender
                });
                itemRec.setText({
                    fieldId: 'custitem_nbs_mountcolour',
                    text: itemDetails.mountColour
                });
                itemRec.setText({
                    fieldId: 'custitem_cseg_nbs_fufilmentp',
                    text: itemDetails.fulfilmentPartner
                });
                itemRec.setText({
                    fieldId: 'custitem2',
                    text: itemDetails.size
                });
                itemRec.setText({
                    fieldId: 'custitem3',
                    text: itemDetails.brand
                });
                itemRec.setText({
                    fieldId: 'custitem1',
                    text: itemDetails.substrateWeight
                });
                itemRec.setText({
                    fieldId: 'custitem4',
                    text: itemDetails.wrap
                });
                itemRec.setText({
                    fieldId: 'custitem6',
                    text: itemDetails.apiVersion
                });
                itemRec.setText({
                    fieldId: 'custitem5',
                    text: itemDetails.quality
                });
                itemRec.setValue({
                    fieldId: 'salesdescription',
                    value: itemDetails.description
                });
                itemRec.setText({
                    fieldId: 'cost',
                    text: itemDetails.price
                });
                itemRec.setText({
                    fieldId: 'currency',
                    text: itemDetails.priceCurrency
                });
                itemRec.setValue({
                    fieldId: 'taxschedule',
                    value: 1
                });
                itemRec.setValue({
                    fieldId: 'isfulfillable',
                    value: true
                });
                itemRec.setValue({
                    fieldId: 'subtype',
                    value: 'Resale'
                });
                var recordID = itemRec.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });
                log.debug("recordID", recordID);
                return recordID;
            } catch (er) {
                log.debug("er @ update", er)
            }
        }
        function post(requestBody) {
            /**
             * Function called upon sending a POST request to the RESTlet.
             *
             * @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
             * or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
             * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
             * @since 2015.2
             */

        }
        /**
         * Function called upon sending a DELETE request to the RESTlet.
         *
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
         * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
         * @since 2015.2
         */
        function doDelete(requestParams) {

        }

        return {
            get: doGet,
            /*put: doPut,*/
            post: doPost,
            updateValue: updateValue,
            /*'delete': doDelete*/
        };

    });

Leave a comment

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