Requirement
The status of the sales invoice is not updating to Paid In Full, if it is applied by a journal and so the sales invoice status will be open even if it is fully applied. Need to create a schedule script for updating the sales invoice status if it is fully applied ie, amount remaining will be zero for that sales invoice.
Solution
A schedule script is created to update the sales invoice status to Paid in full, when the amount remaining is 0 with status not as paid in full.
/**
* @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 0 and status not as paid in full
function invoiceSearch() {
var transactionSearchObj = search.create({
type: "transaction",
filters:
[
["type", "anyof", "Custom105"],
"AND",
["amountremaining", "equalto", "0.00"],
"AND",
["status", "noneof", "Custom105:C"],
// "AND",
// ["internalid", "anyof", "905482"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "statusref", label: "Status"}),
search.createColumn({name: "entity", label: "Name"}),
search.createColumn({name: "amountremaining", label: "Amount Remaining"})
]
});
return transactionSearchObj;
}
//Function called to reschedule the script when count 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: 654,
deploymentId: "customdeploy_jj_ss_sales_inv_stts_aq3330",
params: {
custscript_startrange: startRange,
custscript_endrange: end
}
});
var scriptTaskId = mrTask.submit();
} catch (err) {
log.error({
title: 'error on rescheduleScriptandReturn',
details: err
});
}
}
const execute = (scriptContext) => {
try {
//Get curresnt script
var scriptObj = runtime.getCurrentScript();
log.debug('SCRIPT: ', scriptObj);
//Get value of script parameters
var start = scriptObj.getParameter({name: 'custscript_startrange'}) || 0;
var end = scriptObj.getParameter({name: 'custscript_endrange'}) || 500;
//Calling invoice search with amount remaining greater than 0 and status not as paid in full
var searchResult = invoiceSearch();
var totalCount = searchResult.runPaged().count;
var searchRes = searchResult.run().getRange({
start: start,
end: end
});
for (var i = 0; i < 500; i++) {
try {
//When check for parameter returns true
if (checkForParameter(searchRes[i].getValue({name: "internalid", label: "Internal ID"}))) {
//Load each sales invoice record
var objRecord = record.load({
type: 'customtransaction_jj_sales_invoice',
id: searchRes[i].getValue({name: "internalid", label: "Internal ID"}),
isDynamic: true,
});
log.debug('Objrecord in remaingingusage', objRecord)
// Setting the invoice record status to Paid in Full
var set = objRecord.setValue({
fieldId: 'transtatus',
value: 'C'
});
//Save of the sales invoice record with the updated status
var saverec = objRecord.save()
log.debug('After ', saverec)
log.debug('Set', set)
// }
}
} catch (e) {
log.debug('Error@Getvalue', e)
}
}
//If count greater than 500 reschedule the script
if (totalCount > 500) {
start = Number(start) + 500;
end = Number(end) + 500;
if (totalCount > start) {
rescheduleScriptandReturn(start, end);
}
}
log.debug('After setting start', start)
log.debug('After setting end', end)
} catch (e) {
log.debug('Error@Excute', e)
}
}
return {execute}
});