Get numeric ID of a Custom Record Type with Suitescript 2.x

This week, while working on a SuiteApp we’ve been developing for a client, we stumbled upon an unexpected issue. One of those that should’ve had an easier resolution but that as far as we know it doesn’t.

It’s about getting the numeric (internal ID) of a Custom Record Type when all you have is the text based ID.

We all know that when you create a Custom Record Type in Netsuite, you assign a text ID that ends up reading something like: customrecord_my_awesome_record_type, however, if you open the native page that Netsuite provides out-of-the-box, where you can see the list of instaces for that Record Type, you can see that the page uses a numeric (Plain old internal ID) in the URL to differentiate this Custom Record Type from another one.

In particular, the problem was that we needed one page within the SuiteApp to display a link to the list of instances for my Custom Record Type; however, because the project is a SuiteApp, using the URL that has the numeric ID is impossible because that ID will always change between accounts, therefore, we needed to build the URL within the code to dynamically get the appropriate numeric ID for the record type within each account.

Perhaps putting it this way makes it look simple, the problem is that there’s not (as far as we know) any native way within the Suitescript 2.x API to get a Custom Record Type’s numeric ID if all you have is the text ID (which does stay fixed across accounts).

The solution, as hacky as it sounds, is to trick Netsuite into thinking that you will be creating a new record instance for your record type, without actually saving the record to Netsuite’s database. You normally achieve this with the “n/record” module, which has a “create” method that accepts the text based ID for the record type (which is the one that we had).

When you use the “create” method (withouth saving the record yet), the return value is an instance of the record type, which is an object that contains, among other properties, the numeric ID that we needed in order to build our URL. So, using this logic to build a function, looks like this:

// Use the text ID to create an instance of the record type.
let temp_record = record.create({
    type: 'customrecord_my_awesome_record_type',
    isDynamic: true,
});

// Access the rectype property, within the fields property of the resulting objects to get the numeric ID and store in a variable.

let temp_record_id = JSON.parse(JSON.stringify(temp_record))['fields']['rectype'];

// Use the newly obtained numeric ID to finish the job.
let temp_record_url = '/app/common/custom/custrecordentrylist.nl?rectype=' + temp_record_id;

We can count this as a clever solution for a problem that shouldn’t be there in the first place but that we needed to solve anyway.

If you are ever stuck fixing/debugging a similar issue, do not hesitate to contact us at hello@entropyservices.com

Relevant Insights

Recent Case Studies