I have been researching for approximately a month on how to update multiple saved searches. I have been tasked with changing the owner of two terminated employees over to a different owner. Everything I have found states that I need to write a script. I have a script that is executing successfully, but not making the modifications in the script.
Can anyone provide any insight?
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record'], function(search, record) {
function execute(context) {
var startDate = '1/01/2021 1:00 am'; // Specify the start date of the date range in yyyy-mm-dd format
var endDate = '7/23/2023 11:00 pm'; // Specify the end date of the date range in yyyy-mm-dd format
var currentOwner1 = '61'; // Specify the current owner internal ID 1
var currentOwner2 = '56'; // Specify the current owner internal ID 2
var currentOwner3 = '79013'; // Specify the current owner internal ID 3
var newOwner = '82130'; // Specify the new owner internal ID
// Create a search to retrieve saved searches within the date range and owned by the current owners
var savedSearchesSearch = search.create({
type: 'savedsearch',
columns: ['internalid', 'Title', 'owner', 'lastrunon'], // Add additional columns as needed
filters: [
['lastrunon', 'within', startDate, endDate],
'AND',
[
['owner', 'anyof', currentOwner1],
'OR',
['owner', 'anyof', currentOwner2],
'OR',
['owner', 'anyof', currentOwner3]
]
]
});
// Run the search and process the results
var searchResult = savedSearchesSearch.run();
searchResult.each(function(result) {
// Access the saved search field values
var savedSearchId = result.getValue({
name: 'internalid'
});
var savedSearchName = result.getValue({
name: 'Title'
});
var currentOwner = result.getValue({
name: 'owner'
});
// Add additional fields as needed
// Check if the current owner is one of the owners to be updated
if (currentOwner === currentOwner1 || currentOwner === currentOwner2 || currentOwner === currentOwner3) {
// Perform any desired operations on the individual saved searches
log.debug('Processing Saved Search', 'ID: ' + savedSearchId + ', Title: ' + savedSearchName);
// Change the owner of the saved search to the new owner
changeSavedSearchOwner(savedSearchId, newOwner);
}
// Continue processing remaining saved searches
return true;
});
}
/**
* Change the owner of the saved search with the specified ID.
* @param {string} savedSearchId - The internal ID of the saved search.
* @param {string} newOwner - The new owner internal ID.
*/
function changeSavedSearchOwner(savedSearchId, newOwner) {
// Load the saved search record
var savedSearch = record.load({
type: search.Type.SAVED_SEARCH,
id: savedSearchId
});
// Set the new owner
savedSearch.setValue({
fieldId: 'owner',
value: newOwner
});
// Save the modified saved search with comments
savedSearch.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
log.debug('Saved Search Owner Change', 'Owner updated for Saved Search ID ' + savedSearchId);
}
return {
execute: execute
};
});
These are the deployment logs: