Scenario:
User wants to create a SuiteScript Saved Search to get the status of the Customer.
Solution:
We will utilize N/search module, to create a saved search. Below is a code snippet demonstrating how to achieve this:
require(['N/search'],function(search){
var soSearch = search.create({
type: search.Type.CUSTOMER,
filters: [{
name: 'companyname',
operator: search.Operator.ISNOTEMPTY
}],
columns:[{
name:'companyname'
},
{
name:'entitystatus'
}],
title: 'Status of Customer'
})
var resultSet=soSearch.run()
resultSet.each(function(result){
console.log('-----------')
console.log('Customer Name: ' + result.getValue({name:'companyname'}))
console.log('Status: ' + result.getText({name:'entitystatus'}))
console.log('-----------')
return true
})
})
- We start by requiring the N/search module, which provides functions for creating and executing saved searches.
- Inside the require function, we create a saved search using the search.create() method. We specify the search type as CUSTOMER to target customer records.
- We set a filter to ensure that the company name is not empty, ensuring we retrieve only relevant records.
- Columns are defined to retrieve the company name (companyname) and the entity status (entitystatus) of the customers.
- We give a title to the saved search for identification purposes.
- The run() method executes the search, and the results are stored in a resultSet.
- We iterate over each result using the each() method. For each customer, we log their name and status.
- Finally, we return true to continue processing remaining results.
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.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to create saved searches in SuiteScript? Feel free to share them here!
If you find this information useful, let us know by reacting or commenting on this post! Thank you!