Home > database >  Status check before sending email
Status check before sending email

Time:12-12

I have the below script, I want to send the email when the task is completed, I have inputted the check task lines exactly as the oracle documentation specifies and the script works fine without these lines, but when I put them in I get the error 'Syntax error: missing ; before statement'

These are the lines causing the problem:

let taskStatus = task.checkStatus(searchTask1);
if (taskStatus.status === 'COMPLETE')

I cannot see where there is anything missing here, can anyone help? Thank you

/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/

define(['N/task','N/email'],

/**
* @param {record} record
* @param {search} search
*/

function(task, email) {

var FILE_ID = 433961; 
var SEARCH_ID = 1610;

function execute(scriptContext) {

var searchTask1 = task.create({
taskType: task.TaskType.SEARCH

});

searchTask1.savedSearchId = SEARCH_ID;
searchTask1.fileId = FILE_ID;

var searchTaskId1 = searchTask1.submit();

let taskStatus = task.checkStatus(searchTask1);
if (taskStatus.status === 'COMPLETE'){
email.send({
            author: 3499,
            recipients: 'An email address',
            subject: 'A subject',
            body: 'body text',
        });
}

CodePudding user response:

You'll need to execute your scheduled script again. When you execute the scheduled script, save the taskId somewhere (file cabinet, for example) and then once the task is complete, delete it. You probably also want to put a counter in somewhere so that the scheduled script doesn't keep running, should something go wrong. Something like this;

function(task, email) {

    var FILE_ID = 433961;
    var SEARCH_ID = 1610;

    function execute(scriptContext) {
        var shouldExecuteThis = false;
        var searchTask1 = task.create({
            taskType: task.TaskType.SEARCH
        });

        searchTask1.savedSearchId = SEARCH_ID;
        searchTask1.fileId = FILE_ID;

        var searchTaskId1 = searchTask1.submit();
        // TODO: Save the searchTaskId1 somewhere so we can check the status later. (File cabinet to a text file.)
        if (searchTaskId1) {
            var searchTaskStatus = task.checkStatus({taskId: searchTaskId1});
            // Still pending, so we need to check again later.
            if (searchTaskStatus.status === task.TaskStatus.PENDING || searchTaskStatus.status === task.TaskStatus.PROCESSING) {
                shouldExecuteThis = true;
            }
            // The task has completed, update the configuration file and log the results.
            if (searchTaskStatus.status === task.TaskStatus.COMPLETE) {
                // Delete the searchTaskId1 from where is it saved.
                // SEND EMAIL
                email.send({
                    author: 3499,
                    recipients: 'An email address',
                    subject: 'A subject',
                    body: 'body text',
                });
            }
            if (shouldExecuteThis) {
                var thisTaskId = '';
                var thisTask = task.create({
                    taskType: task.TaskType.SCHEDULED_SCRIPT,
                    scriptId: runtime.getCurrentScript().id,
                    deploymentId: runtime.getCurrentScript().deploymentId
                });

                try {
                    thisTaskId = thisTask.submit();
                } catch (e) {
                    log.error('TASK FAILED', e);
                }
            }
        }
    }
}
  • Related