This function allows users to dynamically construct a URL for a saved search in NetSuite and open the search results in a new browser tab. It uses the `currentRecord` and `url` modules from NetSuite’s SuiteScript 2.x API and is designed for flexibility, reusability, and ease of integration.
Function Overview:
The `openSavedSearchResults` function:
– Dynamically retrieves the current record context.
– Constructs a URL for a specific saved search, using parameters like the current record ID and additional filters.
– Opens the search results in a new browser tab.
This function is ideal for scenarios where you need to link records to related searches, such as transactions, customers, or custom records.
/**
* Function: openSavedSearchResults
* Description: Dynamically constructs a URL to a saved search results page based on the current record
* and specified parameters, then opens it in a new browser tab.
*
* Modules Required:
* - currentRecord: To retrieve the current record being viewed or edited.
* - url: To construct domain-specific and task-specific URLs in NetSuite.
*
* Parameters:
* @param {Object} obj - Object containing the required parameters for the function.
* @param {string} obj.searchid - The internal ID of the saved search to execute.
* @param {string} [obj.searchtype='Transaction'] - The type of search (e.g., 'Transaction', 'Customer').
* @param {Object} [obj.additionalParams={}] - Additional parameters to customize the search URL.
*
* Usage Example:
* ```javascript
* openSavedSearchResults({
* searchid: 'customsearch_opportunity_search',
* searchtype: 'Transaction',
* additionalParams: { custom_field_id: 'custom_value' }
* });
* ```
*
* Notes:
* - Ensure the saved search (searchid) exists in the system.
* - The current record must be accessible through the `currentRecord` module.
*/
function openSavedSearchResults(obj) {
try {
// Validate input parameters
if (!obj || !obj.searchid) {
throw new Error('The "searchid" parameter is required.');
}
// Load required modules
const rec = currentRecord.get();
const url = require('N/url');
if (!rec) {
throw new Error('No current record context found.');
}
// Construct URL components
const scheme = 'https://';
const host = url.resolveDomain({
hostType: url.HostType.APPLICATION // Determines the appropriate NetSuite domain
});
// Define base search parameters
const sourceSearchParams = {
searchtype: obj.searchtype || 'Transaction', // Default to 'Transaction' if not provided
style: 'NORMAL', // Standard display style for search results
searchid: obj.searchid, // Saved search ID
id: rec.id, // Add the current record ID as a parameter (optional, customizable)
};
// Add any additional parameters passed in obj.additionalParams
if (obj.additionalParams && typeof obj.additionalParams === 'object') {
Object.assign(sourceSearchParams, obj.additionalParams);
}
// Resolve the task link for the saved search
const link = url.resolveTaskLink('LIST_SEARCHRESULTS', sourceSearchParams);
// Construct the full URL
const sourceSearchUrl = scheme + host + link;
// Open the search results in a new browser tab
window.open(sourceSearchUrl, '_blank').focus();
} catch (error) {
// Log error or alert the user
console.error('Error in openSavedSearchResults:', error.message);
alert('An error occurred: ' + error.message);
}
}