Status check before sending email with the help of suite script

Status check before sending email with the help of suite script 2.0

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);
                }
            }
        }
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *