Vendor addresses are an essential component of seamless business operations, serving as a foundation for accurate invoicing, efficient logistics, and timely shipments. Properly maintained vendor address details help prevent errors, streamline communication, and ensure that business transactions run smoothly from start to finish.
In NetSuite, to retrieve vendor address data in using SuiteScript, you would typically need to perform a saved search or a custom search to access the information you need. Below is an example of how you can write a SuiteScript 2.x to create a saved search and extract vendor address information.
require(['N/search'],function(search){
var trSearch = search.create({
type: search.Type.VENDOR,
columns:[{
name:'entityid'
},
{
name:'companyname'
},
{
name:'addresslabel'
},
{
name:'city'
},
{
name:'state'
},
{
name:'country'
},
{
name:'zipcode'
}],
title: 'Address of Vendor'
})
var resultSet=trSearch.run()
resultSet.each(function(result){
console.log('-----------')
console.log(Vendor ID : ' + result.getValue({name:'entityid'}))
console.log('Customer : ' + result.getValue({name:'companyname'}))
console.log('Address 1 : ' + result.getValue({name:'addresslabel'}))
console.log('City : ' + result.getValue({name:'city'}))
console.log('State : ' + result.getValue({name:'state'}))
console.log('Country : ' + result.getValue({name:'country'}))
console.log('Zip Code : ' + result.getValue({name:'zipcode'}))
console.log('-----------')
return true
})
})
Note: You can extend the search by adding more columns or filters, such as vendors located in specific countries or those with specific account settings.
Here’s a breakdown of the script:
1. Module Import:
- The script begins by using require(['N/search'], function(search) {...}), which imports the N/search module, which is used for creating and running searches within NetSuite.
2. Search Creation:
- Inside the function, the search.create() method is used to create a new search.
- Type: search.Type.VENDOR indicates that this search will focus on the "Vendor" record type.
- Columns: An array of column definitions is provided to retrieve specific data from the results:
- entityid: Vendor ID.
- companyname: Vendor’s company name.
- addresslabel: Vendor’s address label.
- city: City of the vendor's address.
- state: State of the vendor's address.
- country: Country of the vendor’s address.
- zipcode: Zipcode of the vendor’s address.
- Title: The title of this search is labeled 'Address of Vendor'.
3. Executing the Search:
- var resultSet = trSearch.run() executes the search and stores the results in resultSet.
4. Iterating Through Results:
- The .each() method is used to iterate through the search results. The function provided to .each() will be executed for each result.
- Inside the each loop, result.getValue({name:'<field_name>'}) is used to retrieve the field values of the current result.
- For example:
- result.getValue({name: 'entityid'}): Retrieves the Vendor ID or Entity ID.
- result.getValue({name: 'companyname'}): Retrieves the Company Name of the vendor.
- result.getValue({name: 'addresslabel'}): Retrieves the Address Label (typically a line of the address like "Street Address", "Billing Address").
- result.getValue({name: 'city'}): Retrieves the City of the vendor's address.
- result.getValue({name: 'state'}): Retrieves the State or Province of the vendor's address.
- result.getValue({name: 'country'}): Retrieves the Country of the vendor's address.
- result.getValue({name: 'zipcode'}): Retrieves the Zip Code or Postal Code of the vendor's address.
5. Logging Results: For each result, various details about the vendor are logged to the console:
- Vendor ID (Vendor ID)
- Customer (Company Name)
- Address 1 (Address Label)
- City, State, Country, Zip Code
- These values are preceded by separator lines (console.log('-----------')) for better visibility.
6. Continuation of Search Loop:
- return true ensures that the loop continues processing all 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!