can I create a saved search in a schedule script a...
# ask-stanley-ai
l
can I create a saved search in a schedule script and set the owner to an specific user? also, how can I delete a save search in a suitescript?
c
AI generated answer (using internal Slack threads):
****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ****** Yes, you can create a saved search in a scheduled script and set the owner to a specific user. Here is a sample SuiteScript code that demonstrates how to do this:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 */
define(['N/search', 'N/runtime', 'N/record'], function(search, runtime, record) {
    function execute(context) {
        // Set the new owner's internal ID
        var newOwnerId = '12345';

        // Get the list of saved searches to update
        var savedSearchList = getSavedSearchesToUpdate();

        // Iterate through the saved searches and update the owner
        for (var i = 0; i < savedSearchList.length; i++) {
            try {
                var savedSearchId = savedSearchList[i];
                // Load the saved search record
                var savedSearchRecord = record.load({
                    type: record.Type.SAVED_SEARCH,
                    id: savedSearchId
                });

                // Update the owner field with the new owner's internal ID
                savedSearchRecord.setValue({
                    fieldId: 'owner',
                    value: newOwnerId
                });

                // Save the updated record
                savedSearchRecord.save();

                log.debug('Updated Saved Search', 'ID: ' + savedSearchId);
            } catch (e) {
                log.error('Error Updating Saved Search', 'ID: ' + savedSearchId + ' | Error: ' + e.message);
            }
        }
    }

    function getSavedSearchesToUpdate() {
        // Replace this with the search criteria or the list of saved search IDs you want to update
        var savedSearchList = [
            'search_id_1',
            'search_id_2',
            // ...
        ];
        return savedSearchList;
    }

    return {
        execute: execute
    };
});
Replace '12345' with the internal ID of the new owner and update the getSavedSearchesToUpdate() function to return the list of saved search IDs you want to update. Once you have updated the script, create a new Scheduled Script record in NetSuite and deploy it to run the script. However, it's important to note that the Saved Search record type is not fully exposed to SuiteScript, and there may be some limitations or unexpected behavior. As for deleting a saved search in SuiteScript, unfortunately, there is no direct way to delete a saved search using SuiteScript. The Saved Search record type does not support the delete operation in SuiteScript. You would have to manually delete the saved search from the NetSuite UI.
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.873588681
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.857588291
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.857412934
Please provide your feedback on this answer.