NetSuite: External ID Search Utility (Enhanced)

Published on August 28, 2022.

Last week I shared a NetSuite Suitelet that can be used to locate transactions and entities based on an external ID. I've received a lot of nice feedback about that blog post, and I really appreciate it.

Several readers wrote to me and asked if it would be possible to enhance the script so that it supports locating items based on their external IDs as well. That required a few small changes to the original script, and I'm including the enhanced script at the end of this post.

Here's an animation showing a search for items based on their external IDs.

Click the image to view a larger version.

A Summary of the Changes

Here's a quick summary of the changes that I made to the original script.

First, I added this subquery to the SuiteQL query, and its rows are appended to the recordset using the UNION operator.

SELECT
	'<a href="/app/common/item/item.nl?id=' || ID || '" target="_item" || ID || ">View</a>' 
		|| ' | ' ||
		'<a href="/app/common/item/item?id=' || ID || '&e=T" target="_item" || ID || ">Edit</a>' 
		AS Links,
	ExternalID,
	FullName AS Name,
	CreatedDate AS DateCreated,												
	BUILTIN.DF( ItemType ) AS RecordType,
	ID AS InternalID,
	ItemID AS RecordID
FROM
	Item
WHERE
	ExternalID LIKE ?

Next, I adjusted the call to the runSuiteQL method, so that it includes a third "externalID" parameter.

var queryResults = query.runSuiteQL(
	{
		query: theQuery, 
		params: [ externalID, externalID, externalID ]
	}
); 

And that's all there was to it.

Wrapping Up

I want to thank the NetSuite community for the feedback that I received from my previous post, and for all of the nice feedback that I've been getting lately about my blog in general. I really do appreciate it.

About Me

Hello, I'm Tim Dietrich. I develop custom software for businesses that are running on NetSuite, including mobile apps, Web portals, Web APIs, and more.

I'm the developer of several popular NetSuite open source solutions, including the SuiteQL Query Tool, SuiteAPI, and more.

I founded SuiteStep, a NetSuite development studio, to provide custom software and AI solutions - and continue pushing the boundaries of what's possible on the NetSuite platform.

The Suitelet

To download the script, click here.

/**
* @NApiVersion 2.1
* @NScriptType Suitelet
* @NModuleScope Public
*/

/* 

------------------------------------------------------------------------------------------
Script Information
------------------------------------------------------------------------------------------

Name:
External Search Utility

ID:
_external_id_search

Description
A utility for searching transactions and entities based on an external ID.


------------------------------------------------------------------------------------------
MIT License
------------------------------------------------------------------------------------------

Copyright (c) 2022 Timothy Dietrich.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


------------------------------------------------------------------------------------------
Developer(s)
------------------------------------------------------------------------------------------

Tim Dietrich
• timdietrich@me.com
• https://timdietrich.me


------------------------------------------------------------------------------------------
History
------------------------------------------------------------------------------------------

20220822 - Tim Dietrich
• Initial version.

20220828 - Tim Dietrich
• Added support for items.

*/


var 
	log,
	query,
	serverWidget;


define( [ 'N/log', 'N/query', 'N/ui/serverWidget' ], main );


function main( logModule, queryModule, serverWidgetModule ) {

	log = logModule;
	query= queryModule;
	serverWidget = serverWidgetModule;				
	
    return {
    
    	onRequest: function( context ) {
    	
			var form = serverWidget.createForm(
				{
					title: 'External ID Search Utility',
					hideNavBar: false
				}
			);		
			
			form.addSubmitButton( { label: 'Search' } );
			
			var idField = form.addField(
				{
					id: 'custpage_field_externalid',
					type: serverWidget.FieldType.TEXT,
					label: 'External ID'
				}								
			);				
			
			idField.isMandatory = true;	
											
			if ( context.request.method == 'POST' ) {	
			
				idField.defaultValue = context.request.parameters.custpage_field_externalid;

				formProcess( context, form );
			
			}
			
			context.response.writePage( form );	
			
        }
        
    }

}


function formProcess( context, form ) {	

	var externalID = context.request.parameters.custpage_field_externalid;

	var theQuery = `
			SELECT
				*
			FROM
				(
	
					SELECT
						'<a href="/app/accounting/transactions/transaction.nl?id=' || ID || '" target="_transaction" || ID || ">View</a>' 
							|| ' | ' ||
							'<a href="/app/accounting/transactions/transaction.nl?id=' || ID || '&e=T" target="_transaction" || ID || ">Edit</a>' 
							AS Links,
						ExternalID,
						TranDisplayName AS Name,
						CreatedDate AS DateCreated,												
						BUILTIN.DF( Type ) AS RecordType,
						ID AS InternalID,
						TranID AS RecordID									
					FROM
						Transaction
					WHERE
						ExternalID LIKE ?

					UNION

					SELECT
						'<a href="/app/common/entity/entity.nl?id=' || ID || '" target="_entity" || ID || ">View</a>' 
							|| ' | ' ||
							'<a href="/app/common/entity/entity.nl?id=' || ID || '&e=T" target="_entity" || ID || ">Edit</a>' 
							AS Links,
						ExternalID,
						EntityTitle AS Name,
						DateCreated,						
						'Entity' AS RecordType,
						ID AS InternalID,
						EntityID AS RecordID				
					FROM
						Entity
					WHERE
						ExternalID LIKE ?
						
					UNION
					
					SELECT
						'<a href="/app/common/item/item.nl?id=' || ID || '" target="_item" || ID || ">View</a>' 
							|| ' | ' ||
							'<a href="/app/common/item/item?id=' || ID || '&e=T" target="_item" || ID || ">Edit</a>' 
							AS Links,
						ExternalID,
						FullName AS Name,
						CreatedDate AS DateCreated,												
						BUILTIN.DF( ItemType ) AS RecordType,
						ID AS InternalID,
						ItemID AS RecordID
					FROM
						Item
					WHERE
						ExternalID LIKE ?

				)
			ORDER BY
				ExternalID,
				RecordType,
				RecordID
		`;
	
	try {

		var queryResults = query.runSuiteQL(
			{
				query: theQuery, 
				params: [ externalID, externalID, externalID ]
			}
		); 				
	
		var records = queryResults.asMappedResults();				
	
		if ( records.length > 0 ) {	

			var resultsSublist = form.addSublist(
				{ 
					id : 'results_sublist', 
					label : ` External ID References for `, 
					type : serverWidget.SublistType.LIST 
				}
			);

			var columnNames = Object.keys( records[0] );

			for ( i = 0; i < columnNames.length; i++ ) {

				resultsSublist.addField(
					{ 
						id: 'custpage_results_sublist_col_' + i,
						type: serverWidget.FieldType.TEXT,
						label: columnNames[i]
					}
				);

			}

			for ( r = 0; r < records.length; r++ ) {

				var record = records[r];

				for ( c = 0; c < columnNames.length; c++ ) {

					var column = columnNames[c];

					var value = record[column];
					
					if ( value != null ) {
					
						value = value.toString();
						
						if ( value.length > 300 ) {			
							value = value.substring( 0, 297 ) + '...';			
						}

						resultsSublist.setSublistValue(
							{
								id : 'custpage_results_sublist_col_' + c,
								line : r,
								value : value
							}
						);        

					}	
					
				}

			}			

		} else {
		
			var errorField = form.addField(
				{
					id: 'custpage_field_error',
					type: serverWidget.FieldType.TEXT,
					label: 'Error'
				}								
			);		
			
			errorField.defaultValue = 'No references were found for: ' + context.request.parameters.custpage_field_itemid;					
		
		}

	} catch( e ) {	
	
		var errorField = form.addField(
			{
				id: 'custpage_field_error',
				type: serverWidget.FieldType.LONGTEXT,
				label: 'Error'
			}								
		);		
	
		errorField.defaultValue = e.message;			
		
	}

}

Copyright © 2025 Tim Dietrich.