ANS-0913 · SUITESCRIPT DEVELOPMENT

How to Find the Internal ID of a Custom Record by a Specific Field Value

Learn to programmatically retrieve the internal ID of a NetSuite custom record using a field's value as a search criterion.

Short answer

To find a custom record's internal ID based on a specific field value, utilize NetSuite's SuiteScript 2.x search module. Create a search filter for the desired custom field and value, then execute the search. Iterate through the results to extract the internal ID from the matching custom record.

Scenario

A user needs to programmatically identify the internal ID of a NetSuite custom record. The identification must be based on the value of a specific custom field within that record, such as a "SOId" field, to retrieve the corresponding record's unique identifier.

Solution

To retrieve the internal ID of a custom record based on a specific field's value, implement a SuiteScript 2.x search. This involves creating a search filter for the target custom field and its value, then executing the search and extracting the internal ID from any matching results.The following SuiteScript 2.x example demonstrates how to perform this operation:

javascript/** * @NApiVersion 2.1 * @NModuleScope SameAccount */define(['N/search'], function(search) {    function getCustomRecordInternalId(customRecordTypeId, customFieldId, fieldValue) {        var internalId = null;        var filters = [];        filters.push(search.createFilter({            name: customFieldId,            operator: search.Operator.IS,            values: fieldValue        }));        var customRecordSearch = search.create({            type: customRecordTypeId, // e.g., 'customrecord_xxx'            filters: filters,            columns: ['internalid']        });        customRecordSearch.run().each(function(result) {            internalId = result.id; // Access the internal ID directly            return false; // Stop after the first match        });        return internalId;    }    // Example usage:    // var recordId = getCustomRecordInternalId('customrecord_mycustomrecord', 'custrecord_soid', 'XXX');    // log.debug('Found Internal ID', recordId);    return {        getCustomRecordInternalId: getCustomRecordInternalId    };});

In this script, customRecordTypeId should be replaced with the script ID of your custom record (e.g., customrecord_xxx), customFieldId with the script ID of the relevant custom field (e.g., custrecord_soid), and fieldValue with the specific data you are searching for (e.g., XXX). The result.id property is used to directly access the internal ID of the found custom record within SuiteScript 2.x search results.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant