Sales orders are important documents that track customer purchases. They help businesses manage transactions, from the order to delivery and payment. By tracking these orders, companies can understand customer preferences, buying trends, and sales patterns, which leads to better decisions and improved service.
In NetSuite, SuiteScript serves as a powerful tool for customizing and automating business processes, allowing businesses to tailor the system to their specific needs. One common use case is retrieving sales orders along with key details such as their internal IDs, associated customers and total amounts, which can then be used for reporting, analysis, or further processing.
This SuiteScript example retrieves and logs key details of sales orders in NetSuite, including the transaction ID, customer, and total amount.
require(['N/search'],function(search){
var soSearch = search.create({
type: search.Type.TRANSACTION,
filters: [['type',search.Operator.IS,'SalesOrd']
,'and',
[ 'mainline', search.Operator.IS, 'T']],
columns:[{
name:'tranid'
},
{
name:'entity'
},
{
name:'total'
}],
title: 'Sales Orders'
})
var resultSet=soSearch.run()
resultSet.each(function(result){
console.log('-----------')
console.log('Transaction ID : ' + result.getValue({name:'tranid'}))
console.log('Customer : ' + result.getText({name:'entity'}))
console.log('Total : ' + result.getValue({name:'total'}))
console.log('-----------')
return true
})
})
Here's a breakdown of the code:
1.require(['N/search'], function(search){
- This line is using RequireJS to load the search module from NetSuite's SuiteScript 2.0 API.
- The search module is used to create and execute searches within NetSuite.
2. search.create(): This creates a saved search.
- type: The type of records to search for. In this case, it’s search.Type.TRANSACTION, which targets transaction records like Sales Orders.
- filters: Filters are applied to the search to narrow down the results.
- First filter: 'type' is the type of transaction, and it's being filtered for 'SalesOrd' (Sales Orders).
- Second filter: 'mainline' is filtered to T, meaning the main line of the transaction (usually the header).
- columns: These are the fields to include in the search results.
- tranid: Transaction ID (Sales Order number).
- entity: Customer associated with the Sales Order.
- total: Total amount of the sales order.
- title: The title of the search, in this case, 'Sales Orders'.
3. soSearch.run(): This method executes the search and returns a resultSet which contains the results of the query.
4. Search Results:
- each(): This method iterates over each result in the resultSet.
- Inside the loop, the following information is logged:
- result.getValue({ name: 'tranid' }): Retrieves the transaction ID (Sales Order number).
- result.getText({ name: 'entity' }): Retrieves the name of the customer (instead of the internal ID).
- result.getValue({ name: 'total' }): Retrieves the total amount of the Sales Order.
- Each result is logged with separators (-----------).
- return true: This is required to continue to the next result.
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
How do you use Search Module Suitescript in your business? Share them in the NetSuite Admin Corner.