Suitelet API to fetch Country and State List from NetSuite

Client needs to fetch the country and state list from NetSuite and need to display it in the website

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/error', 'N/http', 'N/record', 'N/search'],
    /**
 * @param{error} error
 * @param{http} http
 * @param{record} record
 * @param{search} search
 */
    (error, http, record, search) => {

        function stateSearch(){
            try{
                var stateSearchObj = search.create({
                    type: "state",
                    filters:
                        [
                            ["inactive","is","F"]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "id",
                                sort: search.Sort.ASC,
                                label: "Id"
                            }),
                            search.createColumn({name: "fullname", label: "Full Name"}),
                            search.createColumn({name: "shortname", label: "Short Name"}),
                            search.createColumn({name: "country", label: "Country"}),
                            search.createColumn({name: "inactive", label: "Inactive"})
                        ]
                });
                var searchResultCount = stateSearchObj.runPaged().count;
                let mainArray={}
                log.debug("stateSearchObj result count",searchResultCount);
                stateSearchObj.run().each(function(result){
                    let country=result.getText({name: "country", label: "Country"});
                    let state=result.getValue({name: "fullname", label: "Full Name"});
                    if(mainArray.hasOwnProperty(country)){
                        mainArray[country].push(state)
                    }
                    else{
                        mainArray[country]=[];
                        mainArray[country].push(state)
                    }
                    return true;
                });
                return mainArray
            }catch (e) {
                log.debug("error@stateSearch",e)
            }
        }

        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */
        const onRequest = (scriptContext) => {
            try{
                if(scriptContext.request.method=='POST'){
                    let countryList=stateSearch();
                    log.debug('countryList',countryList)
                    let responseBody={
                        'status': 200,
                        'message': 'County and State List',
                        'isSuccess': true,
                        details: countryList
                    }
                    return scriptContext.response.write(JSON.stringify(responseBody))
                }

            }
            catch (e) {
                log.debug("error@onRequest",e)
            }
        }

        return {onRequest}

    });

Leave a comment

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