Requirement
Update the status of sales invoice to open ,when it is in paid in full status with Amount Remaining greater than 0.
Solution
/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
*/
define(['N/currentRecord', 'N/record', 'N/runtime', 'N/search', 'N/task'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{runtime} runtime
* @param{search} search
* @param{task} task
*/
(currentRecord, record, runtime, search, task) => {
/**
* Defines the Scheduled script trigger point.
* @param {Object} scriptContext
* @param {string} scriptContext.type - Script execution context. Use values from the scriptContext.InvocationType enum.
* @since 2015.2
*/
function checkForParameter(parameter) {
try {
if (parameter != "" && parameter != null && parameter != undefined && parameter != "null" && parameter != "undefined" && parameter != " " && parameter != false) {
return true;
} else {
log.debug("Empty Value found");
return false;
}
} catch (e) {
log.debug("Error @ empty check Function: ", e.name + ' : ' + e.message)
}
}
//Sales invoice search created to retrieve the sales invoices when the amount remaining is greater than 0 and status is as paid in full
function invoiceSearchPaidStatus() {
var transactionSearchObj1 = search.create({
type: "transaction",
filters:
[
["type", "anyof", "Custom105"],
"AND",
["amountremaining", "greaterthan", "0.00"],
"AND",
["status", "anyof", "Custom105:C"],
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "entity", label: "Name"}),
search.createColumn({name: "amountremaining", label: "Amount Remaining"}),
search.createColumn({name: "statusref", label: "Status"})
]
});
return transactionSearchObj1;
}
//Function to reschedule the script when the count is greater than 500
function rescheduleScriptandReturn(startRange, end) {
try {
//Startrange is value from which it is resheduled
//End is the limit to which it is rescheduled
log.debug("startRange: ", startRange)
log.debug("limit: ", end)
log.debug("Rescheduling")
var mrTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: 779,
deploymentId: "customdeploy_jj_ss_updt_inv_stsopen_3330",
params: {
custscript_startrange1: startRange,
custscript_endrange1: end
}
});
var scriptTaskId = mrTask.submit();
} catch (err) {
log.error({
title: 'error on rescheduleScriptandReturn',
details: err
});
}
}
const execute = (scriptContext) => {
try {
//Get current script
var scriptObj = runtime.getCurrentScript();
log.debug('SCRIPT: ', scriptObj);
//Get value of script parameters
var start = scriptObj.getParameter({name: 'custscript_startrange1'}) || 0;
var end = scriptObj.getParameter({name: 'custscript_endrange1'}) || 500;
//Calling the invoice search
var searchResult1 = invoiceSearchPaidStatus();
var totalCount1 = searchResult1.runPaged().count;
var searchRes1 = searchResult1.run().getRange({
start: start,
end: end
});
log.debug('Search Result', searchRes1)
for (var j = 0; j < 500; j++) {
try {
//When check for parameter returns true
if (checkForParameter(searchRes1[j].getValue({name: "internalid", label: "Internal ID"}))) {
//Load each sales invoice record
var objRecord1 = record.load({
type: 'customtransaction_jj_sales_invoice',
id: searchRes1[j].getValue({name: "internalid", label: "Internal ID"}),
isDynamic: true,
})
log.debug('2 nd recordddddd', objRecord1)
// Setting the invoice record status to Open
var set1 = objRecord1.setValue({
fieldId: 'transtatus',
value: 'A'
});
//Save of the sales invoice record with the updated status
var saverec = objRecord1.save()
}
} catch (e) {
// log.debug('Error@Getvalue',e)
}
//If count greater than 500 reschedule the script
if (totalCount1 > 500) {
start = Number(start) + 500;
end = Number(end) + 500;
if (totalCount1 > start) {
rescheduleScriptandReturn(start, end);
}
}
}
} catch (e) {
log.debug('Error@Excute', e)
}
}
return {execute}
});