Chidi Okwudire IT Professional. ERP Enthusiast. NetSuite Certified (Administrator, SuiteCloud Developer II, and ERP Consultant). Celigo Certified (Level 4+). Passionate About Empowerment Through Knowledge Sharing. Always Eager to Learn.

Tip: Be Careful With Record Types When Performing Entity Lookups via Script

1 min read

NetSuite has an umbrella “Entity” data type which encompasses various, more concrete entity types such as Vendor, Customer, Employee, Contact, Group, Partner, etc.

Transactions have a similar set up where there is a base Transaction type and more concrete subtypes.

In some situations, it is necessary to define a custom field as an Entity instead of a more concrete type. While working on a recent project, I needed a custom field that could hold either a Vendor or a Customer. Thus, I naturally set it up as an Entity:

However, when performing a lookup of the field in script, I observed that the record type returned did not match expectations. Note that I needed to look up the concrete record type in order to load the record and retrieve fields that are not exposed in the base Entity type.

Here’s the sample script:

var recordTypeLookup = search.lookupFields({
    type: 'entity',
    id: recId,
    columns: 'type'
});
log.debug(funcName, 'Record type: ' +  JSON.stringify(recordTypeLookup));

// Output for a Vendor entity: 
// Record type: {"type":[{"value":"Vendor","text":"Vendor"}]}

// Output for a Customer entity: 
// Record type: {"type":[{"value":"CustJob","text":"Customer"}]}

var recordType = recordTypeLookup.type[0].value;
var entityRec = record.load({
    type: recordType, // Fails for customers as NS expect 'Customer' here instead of 'CustJob'
    id: recId
});

Things went fine when a vendor record was selected. However, upon selecting a customer, I got the following error: The record type [CUSTJOB] is invalid. Ahhhh!

It turns out that CustJob instead of Customer is returned as the type for customer lookups which trips the system. However, the real issue was that I was using the wrong field. Instead of type, I needed to use recordtype for my lookup! Thanks again to Elie Ciment, my buddy at Prolecto, who pointed this out and inspired a revision of the original article.

Here’s the updated script:

var recordTypeLookup = search.lookupFields({
    type: 'entity',
    id: recId,
    columns: 'recordtype'
});
log.debug(funcName, 'Record type: ' +  JSON.stringify(recordTypeLookup));

// Output for a Vendor entity: 
// Record type: vendor

// Output for a Customer entity: 
// Record type: customer

var entityRec = record.load({
    type: recordTypeLookup.recordtype,
    id: recId
});

About CustJob

Apparently, the customer type is called CustJob because Customers and Projects are kinda the same record. This is evident from the URL of a customer record e.g.

https://<account_id>.app.netsuite.com/app/common/entity/custjob.nl?id=123

versus the URL of a vendor record e.g.

https://<account_id>.app.netsuite.com/app/common/entity/vendor.nl?id=456.

Somehow, that nuance slipped through to the script APIs.

I did not check but something similar may be at play for other entity types. Gladly, the solution is very easy.


Now that you know, it’s your turn to share your “favorite” NetSuite API inconsistency in the comments section.


Related Posts

NetSuite Insights is on a mission to raise the standards around NetSuite practices, one insight at a time. If that resonates with you, learn how you can become an author/collaborator here.

Don’t miss a beat – subscribe to our no-nonsense email list to have these game-changing insights hit your inbox as soon as they’re published. Ignorance? Nah, you’ve got a smarter option. Choose wisdom, choose insights!

Chidi Okwudire IT Professional. ERP Enthusiast. NetSuite Certified (Administrator, SuiteCloud Developer II, and ERP Consultant). Celigo Certified (Level 4+). Passionate About Empowerment Through Knowledge Sharing. Always Eager to Learn.

Leave a Reply

Your email address will not be published. Required fields are marked *

×