NetSuite Saved Searches

Version 0.0.1

📘

Checkout the Oracle docs onRESTlets for more information

Set up


Creating and Deploying a Script

You must be a NetSuite Administrator to create a RESTlet

  1. RESTlet script (save as a file nb_kleene_data_int_rl.js):
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
/*
*
* Name         : nb_kleene_data_int_rl.js
* Property of  : NoBlue
* Comments     :
*
* Version      Date               Author           Remarks
* 1.0          24/03/2023         NoBlue(GN)      Initial development
*
* */
define(['N/log', 'N/record', 'N/search'],
    /**
 * @param{log} log
 * @param{record} record
 * @param{search} search
 */
    (log, record, search) => {
        /**
         * Defines the function that is executed when a GET request is sent to a RESTlet.
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters passed as an Object (for all supported
         *     content types)
         * @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
         *     Object when request Content-Type is 'application/json' or 'application/xml'
         * @since 2015.2
         */
        const get = (requestParams) => {

            var opsType,
                results = [],
                slice = [],
                i = 0,
                resSearch,
                resultSet,
                searchID;

            opsType = requestParams.type;

            log.debug('opsType', opsType);

            //GopiNadendla_2023-03-26: -- check for type
            if(opsType === 'search'){

                //GopiNadendla_2023-03-26: -- check for all seaved searches available in suystem
                var savedsearchSearchObj = search.create({
                    type: "savedsearch",
                    filters:
                        [
                            ["isinactive","is","F"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "title", label: "Title"}),
                            search.createColumn({name: "id", label: "ID"}),
                            search.createColumn({name: "access", label: "Access"}),
                            search.createColumn({name: "recordtype", label: "Type"})
                        ]
                });
                var searchResultCount = savedsearchSearchObj.runPaged().count;
                log.debug("savedsearchSearchObj result count",searchResultCount);

                resultSet = savedsearchSearchObj.run();
            } else if(opsType === 'data'){

                searchID = requestParams.searchId;

                if(searchID){
                    resSearch = search.load({
                        id: searchID
                    });

                    resultSet = resSearch.run();
                } else {
                    return { error : 'No search id passed, please pass parameter as searchId'}
                }

            } else {
                return { error : 'Parameter type is invalid/not passed, please pass parameter as Type'}
            }

            //GopiNadendla_2023-03-26: -- Loop through and get results
            do {
                slice = resultSet.getRange({ start: i, end: i + 1000 });
                slice.forEach(function(row) {
                    var resultObj = {};
                    row.columns.forEach(function(column) {
                        resultObj[column.name] = row.getValue(column);
                    });
                    results.push(resultObj);
                    i++;
                });
            } while (slice.length >= 1000);


            return (results);
        }

        /**
         * Defines the function that is executed when a POST request is sent to a RESTlet.
         * @param {string | Object} requestBody - The HTTP request body; request body is passed 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; returns a string when request Content-Type is 'text/plain'; returns an
         *     Object when request Content-Type is 'application/json' or 'application/xml'
         * @since 2015.2
         */
        const post = (requestBody) => {

        }


        return {get, post}

    });
  1. Create script by going to Customization 🡪 Scripting 🡪 Scripts 🡪 New

  1. Add the script file by clicking on + sign as shown below. A popup will be opened when you click on + sign, please click on Choose File on the popup and attach the file downloaded in the first step. Then click on Create Script Record button. After clicking on ‘Create Script Record’ button, please enter name and id as required and click on save.
  2. After the script is saved you need to verify the 'GET FUNCTION' and 'POST FUNCTION' lists options are selected as presented in the screen below then click 'Deploy Script'
  3. After deployment the screen below will appear. Please set the 'Status' to Released and the 'Log Level' to Debug. Under the Audience tab we recommend choosing 'Select All' for Roles and Employees (if necessary then restrict the Audience to only those who need to run the script, more information on script audiences can be found here). Then save the script.
  4. Once you click on Save as per step 5 you will be taken to the screen below. Copy the External URL as per below screenshot below.(This the URL we will be using to access NetSuite restlet.)

Creating an Integration Record

You must be a NetSuite Administrator to create an integration record

  1. Navigate to Setup 🡪 Integration 🡪 Manage Integrations 🡪 New
  2. Fill in Name(Eg: Kleene Data Integration) and uncheck all checkboxes except Token-Based Authentication as per the screen below. Please then save this Integration.
  3. Once the record is saved, the Consumer Key and the Consumer Secret will be generated. Please save these somewhere secure and accessible as these details will be required when creating the source in the Kleene app and they will not be visible on this page again.

Creating Access Tokens

You must be a NetSuite Administrator to create Access Tokens

  1. In the settings portlet on home screen of NetSuite with administrator access, Manage access tokens link will be available as shown below.
  2. Click 'New My Access Token' the screen below will appear. In the Application Name dropdown field select the appropriate Integration Record that created as per Creating Integration Record section. After clicking save the Token key and secret values will be generated. Please save these somewhere secure and accessible as these details will be required when creating the source in the Kleene app and they will not be visible on this page again.

Setting Up A Source

  1. Go to 'Connect New Source' within Connections in the Kleene app
  2. Your 'Account ID' can be found if you go to Setup 🡪 Company 🡪 Company Information. Then look for the Account ID (it should be in the right column beneath time zone).
  3. Your domain can be found in the URL when you are logged into your Netsuite account for example if your url reads https://115493-sb.app.netsuite.com/appthen your domain is 115493-sb
  4. Your Script ID and Deploy can be taken from the External URL presented on the final screen of deploying a script (see above). Take the value of each after the = sign
  5. Your Consumer Key and Consumer Secret should be retrieved from the secure location chosen after creating an integration record (see above)
  6. Your Token and Token Secret should be retrieved from the secure location chosen after creating an Access Token (see above)

Features


FeatureSupportNotes
Backfillℹ️No Limitations. Any Saved Search the user has access to can be retrieved.
IncrementalIncremental is not supported on either report
API reliability🟢Reliable

Reports detail


⬇️ Report🔑 Incremental key🔑 Primary key📄 Description
SearchesN/AIDReturns all the saved searches your user can access
DataCREATEDIDReturns all the data within the saved search specified by the Search ID in the extract configuration