Search code examples
functionemailscheduled-tasksnetsuitesuitescript

2nd Function not executing - suitescript


I have the below code. I want the function called execute2 to run after the function execute. When I put the script task inside the first function and don't include execute2 then it all runs fine and the second separate script runs. But when I run it in the below format it does not. What is preventing execute 2 from actually running here?

Thank you

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
 
 define(['N/task'],
 
 /**
 * @param {record} record
 * @param {search} search
 */
 
 function(task) {
 
 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();
 //
 //Next Task
 //
 FILE_ID = 433963;
     SEARCH_ID = 1606;
     var searchTask2 = task.create({
         taskType: task.TaskType.SEARCH
     });
 
     searchTask2.savedSearchId = SEARCH_ID;
     searchTask2.fileId = FILE_ID;
     var searchTaskId2 = searchTask2.submit();
 //
 //Next Task
 //
   FILE_ID = 434489;
     SEARCH_ID = 1637;
     var searchTask3 = task.create({
         taskType: task.TaskType.SEARCH
     });
 
     searchTask3.savedSearchId = SEARCH_ID;
     searchTask3.fileId = FILE_ID;
     var searchTaskId3 = searchTask3.submit();
 //
 // Next Task
 //
   FILE_ID = 434490;
     SEARCH_ID = 1606;
     var searchTask4 = task.create({
         taskType: task.TaskType.SEARCH
     });
 
     searchTask4.savedSearchId = SEARCH_ID;
     searchTask4.fileId = FILE_ID;
     var searchTaskId4 = searchTask4.submit();
 
 }
 
 function execute2(scriptContext){
 
      var scriptTask = task.create({
       taskType: task.TaskType.SCHEDULED_SCRIPT,
       scriptId: "customscript_email_sender",
       deploymentId: "customdeploy_email_sender_d"
     });
 
     var scriptTaskId = scriptTask.submit();
 
 }
 
 return {
 
   
   execute: execute,
   execute2: execute2,
 
 };
 
 });

Solution

  • Building on the comment from W.S., all you need to do is to define two functions that encapsulate your business logic and then call those functions in order within your scheduled script's entry point.

    Below is a simple example, showing the scheduled script executing doFirstTask() followed by doSecondTask(). You can redefine those however you like to meet your own needs.

    /**
     * @NApiVersion 2.x
     * @NScriptType ScheduledScript
     */
     define([], function() {
      return {
        execute: function (context)
        {
          doFirstTask(context);
          doSecondTask(context);
        }
      }
    });
    
    /**
     * This function contains all of the logic you need to execute
     * for the first of your two tasks.
     * 
     * @param context Passed in from the scheduled script's entry point.
     */
    function doFirstTask(context) {
      log.debug({
        title: context.type + ': First Task',
        details: 'The first of many great feats.'
      });
    }
    
    /**
     * This function contains all of the logic you need to execute
     * for the second of your two tasks.
     * 
     * @param context Passed in from the scheduled script's entry point.
     */
    function doSecondTask(context) {
      log.debug({
        title: context.type + ': Second Task',
        details: 'Second, even more impressive than the first.'
      });
    }
    

    Here's a screenshot showing what the output looks like.

    log.debug output from scheduled script