Luis
09/08/2023, 12:15 AMDavid B
09/08/2023, 1:01 AMmichoel
09/08/2023, 4:07 AMmichoel
09/08/2023, 4:09 AM/**
* Calculate the accounting period that should be used on the approved supplier invoice, respecting
* the per subsidiary period lock status. This is needed because this script is executed under
* Administrator role which has "Override Period Restrictions" permission, and could potentially
* approve invoices in a locked period if not for this check.
*
* See CORP-4222 for further details.
*
* Code is loosely based on <https://netsuite.custhelp.com/app/answers/detail/a_id/97841/>
*
* @param {nlobjRecord} record Invoice Record
* @returns {string} Internal ID of the period. This will be the existing period if unlocked,
* otherwise the next unlocked period
*/
APApprovalSuitelet.getPostingPeriod = function (record) {
var subsidiary = record.getFieldValue("subsidiary");
var postingPeriod = record.getFieldValue("postingperiod");
if (!this.isPeriodAPLockedForSubsidiary(postingPeriod, subsidiary)) {
return postingPeriod;
}
var futurePeriods = this.getFutureAccountingPeriods(postingPeriod);
for (var i = 0; i < futurePeriods.length; i++) {
if (!this.isPeriodAPLockedForSubsidiary(futurePeriods[i], subsidiary)) {
return futurePeriods[i];
}
}
throw nlapiCreateError(
"ERROR_CALCULATING_POSTING_PERIOD",
"Unable to find an unlocked posting period."
);
};
/**
* @param {string} postingPeriod Internal ID of Account Period to Check
* @param {string} subsidiary Internal ID of subsidiary to check
* @returns {boolean} True if period is locked for AP transactions for given subsidiary
*/
APApprovalSuitelet.isPeriodAPLockedForSubsidiary = function (postingPeriod, subsidiary) {
var results =
nlapiSearchRecord(
"taskitemstatus",
undefined,
[
["itemtype", "anyof", "PCP_LOCK_AP"],
"AND",
["subsidiary", "anyof", subsidiary],
"AND",
["period", "anyof", postingPeriod],
],
[new nlobjSearchColumn("complete")]
) || [];
if (results.length !== 1) {
return false;
}
return results[0].getValue("complete") === "T";
};
/**
* @param {string} postingPeriod Internal ID of Account Period to check from
* @returns {string[]} Array of Internal IDs for future accounting periods that are not fully locked
* for AP
*/
APApprovalSuitelet.getFutureAccountingPeriods = function (postingPeriod) {
var results =
nlapiSearchRecord(
"accountingperiod",
undefined,
[
["internalidnumber", "greaterthan", postingPeriod],
"AND",
["aplocked", "is", "F"],
"AND",
["isquarter", "is", "F"],
"AND",
["isadjust", "is", "F"],
"AND",
["isyear", "is", "F"],
],
[new nlobjSearchColumn("internalid").setSort(false)]
) || [];
var accountPeriods = results.map(function (result) {
return result.getId();
});
// result has duplicates for some reason, so remove them
return accountPeriods.filter(function (item, pos) {
return accountPeriods.indexOf(item) === pos;
});
};
David B
09/08/2023, 4:10 AMDavid B
09/08/2023, 4:12 AMmichoel
09/08/2023, 4:13 AMmichoel
09/08/2023, 4:15 AMunfortunate if WF can't change the posting period.
so would just have to return a user error, right? and also make sure to check if period is locked FIRST, otherwise you'll potentially have to undo any changes the WF makes during the approval processI don't know how a workflow would even check the lock status (without a Workflow Action Script)
David B
09/08/2023, 4:15 AMLuis
09/10/2023, 1:02 AM