The N/search module specifically provides functions for searching records in NetSuite. You can use it to create and execute searches on various NetSuite record types. Here are some key components and functions associated with the N/search module:
1. search.create(options): This function is used to create a new search object. You provide an options object that defines the search criteria, filters, and columns you want to include in the search.
Example:
var mySearch = search.create({
type: ‘customer’,
filters: [‘entityid’, ‘is’, ‘ABC Corp’],
columns: [‘internalid’, ‘entityid’, ‘companyname’]
});
2. search.run(): This function is called on a search object to execute the search. It returns a search.ResultSet object that contains the search results.
Example:
var resultSet = mySearch.run();
3. search.ResultSet: The result set object contains the search results and provides methods for accessing and manipulating the results.
Example:
resultSet.each(function(result){
var internalId = result.getValue({name: ‘internalid’});
var entityName = result.getValue({name: ‘entityid’});
});
4. search.lookupFields(options): This function is used to retrieve field values from a single record without the need to load the entire record.
Example:
var fields = search.lookupFields({
type: search.Type.CUSTOMER,
id: 123,
columns: [‘internalid’, ‘entityid’, ‘companyname’]
});
5. Search Filters():
- Filters are used to define the criteria for selecting records in a search.
- Filters can be based on field values, formula expressions, and other criteria.
- Examples of filter types include:
search.createFilter(options): Create a new filter.search.Filter: Represents a search filter.
var filter = search.createFilter({
name: ‘internalid’,
operator: search.Operator.EQUALTO,
values: 123
});
6. Search Columns:
- Columns define the fields whose values should be retrieved in the search results.
- You can include standard fields or use formula columns to perform calculations.
- Examples of column types include:
search.createColumn(options): Create a new column.search.Columns: Represents a search column.
var column = search.createColumn({
name: ‘entityid’,
sort: search.Sort.ASC
});
7. Search Joins:
- Joins allow you to include related records in your search results.
- For example, you can include customer-related information when searching for sales orders.
- Examples of join types include:
search.createJoin(options): Create a new join.search.Join: Represents a search join.
var join = search.createJoin({
type: ‘customer’,
field: ‘internalid’,
operator: search.Operator.EQUALTO,
values: ‘entity.internalid’
});
8.Search Types:
- The
search.Typeenum provides a list of record types that can be used in searches. - Common record types include ‘customer’, ‘salesOrder’, ‘vendor’, etc.
var mySearch = search.create({
type: search.Type.CUSTOMER,
filters: […],
columns: […]
});
8. Search Sorting:
- You can specify the sort order for search results using the
search.Sortenum. - Sorting can be applied to columns or joined fields.
var column = search.createColumn({
name: 'entityid',
sort: search.Sort.ASC
});
9. Search ResultSet:
- The
search.ResultSetobject represents the result of a search and provides methods for iterating over the results.
var resultSet = mySearch.run();
resultSet.each(function(result){
var internalId = result.getValue({name: 'internalid'});
var entityName = result.getValue({name: 'entityid'});
// Process the result
});