Skip to content

Instantly share code, notes, and snippets.

@axilaris
Created October 19, 2021 07:42
Show Gist options
  • Save axilaris/70a24cd73b76f3d6b6bbd0f8681518eb to your computer and use it in GitHub Desktop.
Save axilaris/70a24cd73b76f3d6b6bbd0f8681518eb to your computer and use it in GitHub Desktop.
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define([
"N/log",
"N/search",
], function (log, search) {
function post(context) {
log.debug('POST1 Context', context);
return JSON.stringify(getCustomRecords(context));
}
function getCustomRecords(context) {
var mycustomSearch = search.create({
type: search.Type.CUSTOM_RECORD + '1589',
title: 'Search Title',
id: 'customsearch_my_second_so_search',
columns: [{
name: 'id'
}, {
name: 'custrecord3987'
}],
filters: [{
name: 'custrecord3987',
operator: search.Operator.EQUALTO,
values: [context.custrecord3987]
}],
});
return mycustomSearch;
}
return {
post: post,
};
});
...
payload = {
"custrecord3987": "12345678",
}
...
@axilaris
Copy link
Author

it failed at this:
mycustomSearch.run().each(function(result) { <-- myentryRESTLET.js:34:28 is here

Result: {"error" : {"code" : "UNEXPECTED_ERROR", "message" : "{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An unexpected SuiteScript error has occurred","stack":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:34:28)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript error has occurred","userEvent":null,"stackTrace":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:34:28)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":true}"}}
{'X-N-OperationId': '6aa4cb5e-4a46-42ac-9543-b8287d0b89d9', 'NS_RTIMER_COMPOSITE': '576976797:706172746E6572733031312E70726F642E6475622E6E65746C65646765722E636F6D:80', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Type': 'application/json;charset=utf-8', 'Pragma': 'No-Cache', 'Cache-Control': 'No-Cache', 'Expires': '0', 'Content-Length': '722', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Wed, 20 Oct 2021 06:01:36 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.9f561b3a.1634709694.7c9b1c60'}


/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define([
    "N/log",
    "N/search",
], function (log, search) {
    
    function post(context) {
      log.debug('POST1 Context', context);
        return JSON.stringify(getCustomRecords(context));
    }


    function getCustomRecords(context) {
        var mycustomSearch = search.create({
            type: search.Type.CUSTOM_RECORD + '1589',
            title: 'Search Title',
            id: 'customsearch_my_second_so_search',
            columns: [{
                name: 'id'
            }, {
                name: 'custrecord3987'
            }],
            filters: [{
                name: 'custrecord3987',
                operator: search.Operator.EQUALTO,
                values: [context.custrecord3987]
            }],

        });
      var results = [];
      mycustomSearch.run().each(function(result) { <-- myentryRESTLET.js:34:28 is here
            var id = result.getValue({
                name: 'id'
            });
            var field1 = result.getValue({
                name: 'custrecord3987'
            });
            results.push({ id: id, custrecord3987: field1});
            return true;
        });
      return results;
    }


    return {
        post: post,
    };
});

@axilaris
Copy link
Author

it failed at this also:

.each(function (result) { <--- myentryRESTLET.js:30:14 it is here it failed

Result: {"error" : {"code" : "UNEXPECTED_ERROR", "message" : "{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An unexpected SuiteScript error has occurred","stack":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:30:14)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript error has occurred","userEvent":null,"stackTrace":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:30:14)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":true}"}}
{'X-N-OperationId': 'b2d508e1-2c0e-44cc-9079-4a63b80d24d4', 'NS_RTIMER_COMPOSITE': '1615709534:706172746E6572733031312E70726F642E6475622E6E65746C65646765722E636F6D:80', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Type': 'application/json;charset=utf-8', 'Pragma': 'No-Cache', 'Cache-Control': 'No-Cache', 'Expires': '0', 'Content-Length': '722', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Wed, 20 Oct 2021 06:05:41 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.9eeece17.1634709941.cc03592'}


/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define([
    "N/log",
    "N/search",
], function (log, search) {
    
    function post(context) {
      log.debug('POST1 Context', context);
        return JSON.stringify(getCustomRecords(context));
    }

    function getCustomRecords(context) {
        var results = [];
        var mycustomSearch = search
            .create({
                type: search.Type.CUSTOM_RECORD + "1589",
                columns: ["custrecord3987"],
                filters: [
                    [
                        "custrecord3987",
                        search.Operator.EQUALTO,
                        context.custrecord3987,
                    ],
                ],
            })
            .run()
            .each(function (result) {  <--- myentryRESTLET.js:30:14  it is here it failed
                var id = result.id;
                var field1 = result.getValue({
                    name: "custrecord3987",
                });
                results.push({ id: id, custrecord3987: field1 });
                return true;
            });
        return results;
    }

    return {
        post: post,
    };
});

@axilaris
Copy link
Author

axilaris commented Oct 20, 2021

I even tried .getRange() instead of .each() and it failed at this also:

mycustomSearch.run().getRange({ <---- myentryRESTLET.js:33:28 failed here

Result: {"error" : {"code" : "UNEXPECTED_ERROR", "message" : "{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An unexpected SuiteScript error has occurred","stack":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:33:28)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript error has occurred","userEvent":null,"stackTrace":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:33:28)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":true}"}}
{'X-N-OperationId': 'e769a6f1-5794-446f-959f-0cf30171a7a9', 'NS_RTIMER_COMPOSITE': '545832440:706172746E6572733031322E70726F642E6475622E6E65746C65646765722E636F6D:80', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Type': 'application/json;charset=utf-8', 'Pragma': 'No-Cache', 'Cache-Control': 'No-Cache', 'Expires': '0', 'Content-Length': '722', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Wed, 20 Oct 2021 06:12:34 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.9eeece17.1634710351.cc3d189'}


/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define([
    "N/log",
    "N/search",
], function (log, search) {
    
    function post(context) {
      log.debug('POST1 Context', context);
        return JSON.stringify(getCustomRecords(context));
    }

    function getCustomRecords(context) {
        var mycustomSearch = search.create({
            type: search.Type.CUSTOM_RECORD + '1589',
            title: 'Search Title',
            id: 'customsearch_my_second_so_search',
            columns: [{
                name: 'id'
            }, {
                name: 'custrecord3987'
            }],
            filters: [{
                name: 'custrecord3987',
                operator: search.Operator.EQUALTO,
                values: [context.custrecord3987]
            }],

        });
      var results = [];
      mycustomSearch.run().getRange({   <---- myentryRESTLET.js:33:28 failed here
          start: 0,
          end: 50
      });        
        
      return results;
    }

    return {
        post: post,
    };
});

@naschumer
Copy link

naschumer commented Oct 20, 2021

@axilaris Not sure how else to help, I got this code working perfectly in my environment with a different custom record (I had to change search.Operator.EQUALTO to search.Operator.ANYOF, so try that).
Other than that, what type of field is custrecord3987?? It determines what you pass in or the type of operator you should use. Are you passing in the internal id (like what is context.custrecord3987?)

function getCustomRecords(context) {
	var results = [];
	search
		.create({
			type: search.Type.CUSTOM_RECORD + "1589",
			columns: ["custrecord3987"],
			filters: [
				[
					"custrecord3987",
					search.Operator.ANYOF,
					context.custrecord3987,
				],
			],
		})
		.run()
		.each(function (result) {
			var id = result.id;
			var field1 = result.getValue({
				name: "custrecord3987",
			});
			results.push({ id: id, custrecord42: field1 });
			return true;
		});
	return results;
}

@axilaris
Copy link
Author

I tried the above example, it failed at this also:

        .each(function (result) {                  <--- myentryRESTLET.js:30:14 failed here

Result: {"error" : {"code" : "UNEXPECTED_ERROR", "message" : "{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An unexpected SuiteScript error has occurred","stack":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:30:14)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript error has occurred","userEvent":null,"stackTrace":["Error\n at getCustomRecords (/SuiteScripts/myentryRESTLET.js:30:14)\n at Object.post (/SuiteScripts/myentryRESTLET.js:12:31)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":true}"}}
{'X-N-OperationId': '5c6f65b3-1fae-4c11-85d2-a1bbc26b3a60', 'NS_RTIMER_COMPOSITE': '193966437:706172746E6572733031312E70726F642E6475622E6E65746C65646765722E636F6D:80', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Type': 'application/json;charset=utf-8', 'Pragma': 'No-Cache', 'Cache-Control': 'No-Cache', 'Expires': '0', 'Content-Length': '722', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Wed, 20 Oct 2021 14:00:29 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.9f561b3a.1634738425.7fea7622'}

/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define([
    "N/log",
    "N/search",
], function (log, search) {
    
    function post(context) {
      log.debug('POST1 Context', context);
        return JSON.stringify(getCustomRecords(context));
    }

    function getCustomRecords(context) {
        var results = [];
        search
            .create({
                type: search.Type.CUSTOM_RECORD + "1589",
                columns: ["custrecord3987"],
                filters: [
                    [
                        "custrecord3987",
                        search.Operator.ANYOF,
                        context.custrecord3987,
                    ],
                ],
            })
            .run()
            .each(function (result) {                  <--- myentryRESTLET.js:30:14 failed here
                var id = result.id;
                var field1 = result.getValue({
                    name: "custrecord3987",
                });
                results.push({ id: id, custrecord42: field1 });
                return true;
            });
        return results;
    }
    return {
        post: post,
    };
});

@axilaris
Copy link
Author

axilaris commented Oct 20, 2021

@naschumer
custrecord3987 is Free-Form Text
refer to this screenshot -> https://i.imgur.com/zY2LohZ.png

as to the payload, its simply like this :

...
payload = {
 "custrecord3987": "12345678",
}
...

I dont pass in id like in the search.lookupFields example you gave

@naschumer
Copy link

@axilaris
You need to use search.Operator.IS for text fields. Here you go:

function getCustomRecords(context) {
	var results = [];
	search
		.create({
			type: search.Type.CUSTOM_RECORD + "1589",
			columns: ["custrecord3987"],
			filters: [
				[
					"custrecord3987",
					search.Operator.IS,
					context.custrecord3987,
				],
			],
		})
		.run()
		.each(function (result) {
			var id = result.id;
			var field1 = result.getValue({
				name: "custrecord3987",
			});
			results.push({ id: id, custrecord3987: field1 });
			return true;
		});
	return results;
}

@axilaris
Copy link
Author

@naschumer thank you so much, it worked! really really appreciate it.

Out of curiousity, how do you know which operator to use for the types, I'd like to know.

@naschumer
Copy link

naschumer commented Oct 20, 2021

@axilaris
Just from experience! Some times you can just try them until one works, usually IS, EQUALTO or ANYOF if it's one thing equaling another thing.

Another thing that you can do, you can build the Saved Search inside NetSuite using the UI, add the columns and filters as well. Then use this chrome extension:
https://chrome.google.com/webstore/detail/netsuite-search-export/gglbgdfbkaelbjpjkiepdmfaihdokglp

And it will turn that search into suitescript 2.0 code that you can paste into your script. Then you won't even need to think about the syntax of the columns/filters.

Hope this helps!

@naschumer
Copy link

@axilaris
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment