Set a Default Posting Period in a Custom Field
The following sample shows how to set a specific default posting period in a custom transaction field.
For the complete tutorial, see Set a Default Posting Period in a Custom Field.
Important:
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/runtime', 'N/search', 'N/log'], (runtime, search, log) => {
function pageInit(context) {
try {
if (context.mode === 'copy' || context.mode === 'create') {
const record = context.currentRecord;
const searchOpenAccountingPeriods = search.load({
id: 'customsearch_open_accounting_periods'
});
const resultsFromSearch = searchOpenAccountingPeriods.run().getRange({
start: 0,
end: 1
});
record.setValue({
fieldId: 'custbody_preferred_posting_period',
value: resultsFromSearch[0].id
});
record.setValue({
fieldId: 'postingperiod',
value: resultsFromSearch[0].id
});
}
} catch(e) {
log.error({
title: 'ERROR',
details: e
});
}
}
function fieldChanged(context) {
try {
if (context.fieldId === 'custbody_preferred_posting_period') {
const record = context.currentRecord;
const prefPostPeriod = record.getValue({
fieldId: 'custbody_preferred_posting_period'
});
record.setValue({
fieldId: 'postingperiod',
value: prefPostPeriod
});
} else {
return;
}
} catch(e) {
log.error({
title: 'ERROR',
details: e
});
}
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged
};
});