Maira S
Maira S

Reputation: 47

How to pass last processed record id to reschedule a scheduled script in NetSuite?

I am having trouble in rescheduling a scheduled script. I have created script parameter 'last_processed_record_id'. I am not aware of how to pass that lastprocessed id in script parameter and run script from where it left.

In my scheduled script, I am processing Invoice saved search results.Please advice!

define(['N/search', 'N/runtime', 'N/record', 'N/task', 'N/file', 'N/format', 
'SuiteScripts/abc/Lib/abc_lib'], function(search, runtime, record, task, file, 
format, lib) {
    function execute(context) {
               processSearchResults();
        var usage= runtime.getCurrentScript();
        log.debug('Remaining governance units: ' + usage.getRemainingUsage());
            }

       //This function fetches saved search data and creates journal entry record
    function processSearchResults(results, scriptid) {
             
            results.forEach(function(result) {
                  var recId= result.getValue({
                    name: "internalid", label: "Internal ID"
                });
            
                var amount = result.getValue({
                    name: "amount",
                    label: "amount"
                });
            })
        }


       //This function updates created  jounal entries on invoice line level custom JE 
field on invoice.
        function updateInvoice() {
    
    }
    return {
        execute: execute
    }
})

Upvotes: 0

Views: 52

Answers (1)

Ganesh
Ganesh

Reputation: 21

To get last processed record ID for next runs of a scheduled script, follow these steps:

1. Create a Script Parameter

Add a parameter to the script record to store the record ID:

  1. Navigate to the Script Record.

  2. Go to the Parameters subtab.

  3. Click the New Parameter button to create a new parameter.

Example Screenshot for adding a parameter:

Example Screenshot for adding a parameter.

2. Update Your Script Code

You need to handle the parameter in two places:

  1. Pass the parameter during task creation: Use the params object in the task.create API to pass the parameter value when scheduling the script for its next run.
var scriptTask = task.create({
    taskType: task.TaskType.SCHEDULED_SCRIPT
});
scriptTask.scriptId = 'customscript_your_script_id';
scriptTask.deploymentId = 'customdeploy_your_deployment_id';
scriptTask.params = {
    custscript_last_processed_record_id: lastProcessedId // Replace with your parameter ID and value
};
scriptTask.submit();
  1. Retrieve the parameter in the next script execution: In the scheduled script's execution context, use ScriptObject.getParameter to fetch the parameter value.
        var lastProcessedId = runtime.getCurrentScript().getParameter({
            name: 'custscript_last_processed_record_id' // Replace with your parameter ID
        });

        log.debug('Last Processed Record ID', lastProcessedId);

By following these steps, you can store the last processed record ID

Upvotes: 2

Related Questions