To convert a NetSuite Workbook to query code, you can use the `N/query` module to load the workbook and then convert it to SuiteQL. Here’s a step-by-step guide:
Step 1: Load the Workbook
First, you need to load the workbook using its ID. This can be done using the `query.load` method.
Step 2: Convert to SuiteQL
Once the workbook is loaded, you can convert it to SuiteQL using the `toSuiteQL` method.
Step 3: Execute the SuiteQL Query
Finally, you can execute the SuiteQL query to get the results.
Here’s an example script that demonstrates these steps:
define(['N/query'], function(query) {
return {
get: function(context) {
// Load the workbook by its ID
var workbook = query.load('customworkbook237');
// Convert the workbook to SuiteQL
var suiteQLQuery = workbook.toSuiteQL();
var suiteQL = suiteQLQuery.query;
// Execute the SuiteQL query
var resultSet = query.runSuiteQL({ query: suiteQL });
// Process the results
resultSet.results.forEach(function(result) {
log.debug('Result', result);
});
// Return the SuiteQL query string and results
return {
suiteQL: suiteQL,
results: resultSet.results
};
}
};
});
Explanation
1. **Load the Workbook**: The `query.load` method loads the workbook using its ID.
2. **Convert to SuiteQL**: The `toSuiteQL` method converts the workbook to a SuiteQL query.
3. **Execute the SuiteQL Query**: The `query.runSuiteQL` method executes the SuiteQL query and retrieves the results.
This approach allows you to leverage the power of SuiteQL to run complex queries and retrieve data efficiently.