Fetch Company Details — Restlet.

Restlet to fetch the company details from the company id and then send back the JSON file which consists of details of the company. This Restlet is used as Magento backend.

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/runtime', 'N/search'],
    /**
     * @param {record} record
     * @param {runtime} runtime
     * @param {search} search
     */
    function(record, runtime, search) {

        /**
         * 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 {
                var customerEmail = requestBody.email;
                log.debug({ title: "customerEmail", details: customerEmail });

                var customerSearchObj = search.create({
                    type: "contact",
                    filters: [
                        ["email", "is", customerEmail]
                    ],
                    columns: [
                        search.createColumn({
                            name: "entityid",
                            join: "customer",
                            label: "ID"
                        })
                    ]
                });

                var searchResultCount = customerSearchObj.runPaged().count;
                var customerId = [];
                if (searchResultCount > 0) {
                    customerSearchObj.run().each(function(result) {
                        customerId.push(result.getValue({
                            name: "entityid",
                            join: "customer",
                            label: "ID"
                        }));
                        return true;
                    });
                } else {
                    return { customerId: false }
                }
                return { customerId: customerId };

            } catch (e) {
                return "Invalid Input"

            }

        }

        return {
            post: doPost
        };

    });

Leave a comment

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