Hello everyone, I'm currently facing a challenge ...
# suitescript
m
Hello everyone, I'm currently facing a challenge Scheduled Script that I hope someone might be able to help me with. The script is intended to update an existing file in the File Cabinet using data from a saved search. However, I'm encountering an issue where only the headers from the saved search are being written to the file, without any of the data rows.
Copy code
* @NApiVersion 2.1
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(['N/task', 'N/search'], function (task, search) {
    /**
     * Definition of the Scheduled script trigger point.
     *
     * @governance
     *
     * @param {Object} scriptContext
     * @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
     */
    function execute(scriptContext) {
        const WEB_QUERY_FOLDER = 32273;

        //load files from File Cabinet
        var folderSearchObj = search.create({
            type: 'folder',
            filters: [['internalidnumber', 'equalto', WEB_QUERY_FOLDER]],
            columns: [
                search.createColumn({
                    name: 'name',
                    sort: search.Sort.ASC,
                    label: 'Name'
                }),
                search.createColumn({
                    name: 'name',
                    join: 'file',
                    label: 'Name'
                }),
              search.createColumn({
                    name: 'internalid',
                    join: 'file',
                    label: 'Id'
                })
            ]
        });
        var resultsFiles = folderSearchObj.run().getRange({
            start: 0,
            end: 98
        });

        log.debug('resultsFiles.length', resultsFiles.length);
      var searchIds = [391];

            let myTask = task.create({
               taskType: task.TaskType.SEARCH
            });
            myTask.savedSearchId = searchIds[0];
            
             /*myTask.fileId = resultsFiles[i].getValue({
                        name: 'internalid',
                        join: 'file'
                    });*/
            myTask.fileId = 208364;
            log.debug('file id ', myTask.fileId);
            /*myTask.filePath = 'Web Query/' + savedSearchName + '.csv';*/
            let myTaskId = myTask.submit();//100 units
           log.debug('myTask.savedSearchId ', myTask.savedSearchId);
          log.debug('myTaskId', myTaskId);
        //  return false;
       // }
    }

    return {
        execute: execute
    };
});
b
go run the search yourself and make sure there are actually results before running the script
m
I generate the 1st file by suitelet and scheduled script. This one generates without problem. However, when testing this scheduled script to update the file, it doesn’t work correctly
b
the important part is the search
go find it and see how it works
if it does anything based on the current user, it will not work for the system user that is used for scripts that run on a schedule
m
I am admin and the script runs with my user. When I run the saved search I see the results clearly. Furthermore, as I said when launching my first script, the csv file is generated correctly. It's the update that's blocking
b
find the logs of a scheduled script execution that ran on a schedule
look at the user
m
The user is me The saved search was created by me The owner of the script is me My role is admin
b
did you find the logs of a deployment that ran on a schedule
m
Yes the user who ran the schedule is user -4 ( the system I think) my user id is 5 But I go to the deployment and do a save and execute
b
same thing
click save and execute and take a look at the user in the logs
m
I just did it and in the logs the user is -4. I don't see anything special. What I don't understand is that I have a suitelet and a she filed script configured in the same way and it works (the user who launch the script is user -4 too)
b
it shouldnt be
go visit your suitelet (which hopefully is using N/task to task your scheduled script_ and take a look at the scheduled script's deployments logs
m
to be precise I created the suitelet and a 1st scheduled script which obviously uses n task . This allows me to create the saved search in CSV format in my cabinet file folder. So if I query the logs I only see information telling me that it works. On the other hand, the 2nd scheduled script that I sent you and a separate script (for testing purposes) and this one just has to update the file created by suitelet + 1st schedule script. So I'm not sure that checking the first scripts will help me get an answer because this one generates the file I can send you the suitelet and scheduled scripts
b
if you are saying that the 1st script works and the second one doesnt
and the second one is isnt the same as the first
the answer is gonna be make them more similar
m
Nop is not the same
Copy code
/**
@NApiVersion 2.1
@NScriptType ScheduledScript
 */
define(['N/search', 'N/file', 'N/log', 'N/runtime', 'N/task'], function (search, file, log, runtime, task) {
    function execute(scriptContext) {
        try {
            var savedSearchId = runtime.getCurrentScript().getParameter('custscript_saved_search_id');
            var savedSearchName = runtime.getCurrentScript().getParameter('custscript_saved_search_name');

            let myTask = task.create({
               taskType: task.TaskType.SEARCH
            });
           log.debug('savedsearchid', savedSearchId);
            myTask.savedSearchId = savedSearchId;
            myTask.filePath = 'Web Query/' + savedSearchName + '.csv';
            
            let myTaskId = myTask.submit();//100 units
        } catch (e) {
            log.error("Scheduled Script Error", e.toString());
        }
    }

    return {
        execute: execute
    };
});
This is the first schedule script
b
you should be able to manually run that one as well
to make it the system user
if that works, gradually transform your second script into the first line by line until you find the problem
it it doesnt, i emphasize the first thing i said, which is you need to udnerstand how the saved search works for different users
m
the 1st schedule script can't work I got an error message. In fact I have to select the saved search on my suitelet which I want to export in csv. So the schedule script waiting this information
b
you can manually set the script parameters on the deployment you use