Set a Default Posting Period in a Custom Field
This use case shows you how to set a default posting period using a custom transaction body field and then copy that value to the standard Posting Period field. This can help limit the posting period options available to users and avoid potential user error in period selections.
This project is available on the SuiteCloud Project Repository on Oracle Samples GitHub and include complete project customization setup with custom objects, unit tests, package file configuration, and more.
Customization Details
This customization for this use case includes:
-
A custom field (Preferred Posting Period) to select an open accounting period
-
A custom form that disables the standard Posting Period and includes the custom Preferred Posting Period field
-
A saved search used to search for open accounting periods
-
A client script triggered on the pageInit and fieldChanged entry points
Steps in this tutorial to complete this customization:
Before You Begin
The following table lists features, permissions, and other requirements necessary for performing this tutorial and implementing the solution:
Required Features |
The following features must be enabled in your account:
For more information, see Enabling Features. |
Required Permissions |
You will need a role with access to the following:
For more information, see NetSuite Permissions Overview. |
Other Requirements |
None |
Step 1: Create the Custom Field, Form, and Saved Search
This customization uses a custom field. The Preferred Posting Period field is used to select a preferred posting period. This custom field is a list/record type transaction body field with values sourced from the accounting period record.
This customization also uses a custom form. The custom invoice form will disable the standard Posting Period field and move the custom Preferred Posting Period field to the required position.
This customization also uses a saved search. The Preferred Posting Periods saved search searches for the open accounting periods that you want users to be able to select in the custom Preferred Posting Period field. This search allows you to filter out posting periods that may be for organizational purposes only or that may be open for corrections but not meant for new transactions.
To create the custom field:
-
Go to Customization > Lists, Records, Fields > Transaction Body Fields > New.
-
Enter or select values for fields as listed in the following table:
Field
Value
Label
Preferred Posting Period
ID
_preferred_posting_period
Description
This custom field is for the user to select a preferred posting period that will be copied into the standard posting period field.
Type
List/Record
List/Record
Accounting Period
Store Value
Checked
Applies To
Sale
Display
Subtab: Main
-
Click Save.
To customize the invoice form:
-
Go to Customization > Forms > Transaction Forms.
-
Click Customize next to the Standard Product Invoice form.
-
Enter or select values for fields as listed in the following table:
Field
Value
Name
Preferred Posting Invoice
ID
_posting_invoice
NetSuite will prepend ‘custform’ to this ID.
Screen Fields tab > Main
Check Show next to Preferred Posting Period. In the Field Group column, select Primary Information. Doing so will automatically move the field up in the list.
To disable the standard Posting Period field, set its Display Type to Inline Text or Disabled.
Form is Preferred
Check this box if you’d like this custom form to be the default invoice form for all users. In this tutorial, it is not necessary to check this box.
-
Click Save.
To create the saved search:
-
Go to Reports > Saved Searches > All Saved Searches > New.
-
Select Accounting Period.
-
On the Saved Accounting Period Search page, enter a Search Title. For searches that others might have access to, you may want to note that the search is used in a script and should not be edited, such as Preferred Posting Periods***USED IN SCRIPT***.
-
Enter an ID, such as _open_accounting_periods. NetSuite automatically adds customsearch as a prefix.
-
On the Criteria tab, enter the following:
Filter
Description
Closed
No
Period Name
does not start with – Q
Period Name
does not start with – FY
-
On the Results tab, in the Sort By field, select Start Date.
-
Click Save & Run to confirm that your search will run and return the proper results.
For more information about creating custom fields, forms, and searches, see the following help topics:
Step 2: Write the Script
This script sets the custom Preferred Posting Period field with the most recent posting period from your saved search results. It then copies the value selected by the user to the standard Posting Period field.
Script Summary
The following table summarizes the script:
Script: Preferred Posting Period |
|
---|---|
Script Type |
|
Modules Used |
|
Entry Points |
For more information about script types and entry points, see SuiteScript 2.x Script Types.
The Complete Script
This tutorial includes the complete script along with individual steps you can follow to build the script in logical sections. The complete script is provided below so that you can copy and paste it into your text editor and save the script as a .js file (for example, cs_preferredPostingPeriod.js).
If you would rather create your script by adding code in logical sections, follow the steps in Build the Script.
This tutorial script uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript Debugger.
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
};
});
Build the Script
You can write the script using a step-by-step approach that includes the following:
The code snippets included below do not account for indentation. Refer to The Complete Script for suggested indentation.
Start with required opening lines
JSDoc comments and a define
function are required at the top of the script file. The JSDoc comments in this script indicate that it is a SuiteScript 2.1 client script. The script uses three SuiteScript modules specified in the define
statement:
-
N/runtime
-provides access to runtime parameters -
N/search
– allows you to create and run on-demand or saved searches and analyze and iterate through the search results -
N/log
– allows you to log execution details
Start a new script file using any text editor and place the following JSDoc comments and define
function at the top of the file:
This tutorial script uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript Debugger.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/runtime', 'N/search', 'N/log'], (runtime, search, log) => {
});
Create the entry point functions
This script is triggered on the pageInit
entry point when an invoice is created and on the fieldChanged
entry point when the Preferred Posting Period field is changed. Both entry point functions use a try-catch block to log any errors that might occur during script execution. Most of the script code will be placed in the try blocks.
Add the following function definitions and initial try-catch block statements at the top of the define
function:
function pageInit(context) {
try {
} catch(e) {
log.error({
title: 'ERROR',
details: JSON.stringify(e)
});
}
}
function fieldChanged(context) {
try {
} catch(e) {
log.error({
title: 'ERROR',
details: JSON.stringify(e)
});
}
}
Update the standard posting period field
If the user changes the value in the custom Preferred Posting Period field, we want the standard Posting Period field to update as well for reporting consistency, even if the user does not have access to edit the standard field.
Add the following code insides the try block of the fieldChanged
function:
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;
}
Set up field defaults
When an invoice is initiated, we need to know if the transaction is either being copied or created. If so, run the saved search to get the list of open accounting periods that should be available for the custom field.
Add the following code inside the try block of the pageInit
function:
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
});
}
Create the return statement
This script associates the pageInit
function with the pageInit
client script entry point and the functionChanged
function with the functionChanged
client script entry point.
Add the following code immediately above the closing });
in your script:
return {
pageInit: pageInit,
fieldChanged: fieldChanged
};
Save your script file
You need to save your script file so you can load it to the NetSuite File Cabinet. Before you save your script file, you may want to adjust the indentation so that the script is readable. Refer to The Complete Script for suggested indentation.
When you are happy with how your script file reads, save it as a .js file (for example, cs_preferredPostingPeriod.js).
Step 3: Create the Script Record
Now that you’ve completed the script, you can upload the script file to the File Cabinet and create a script record for it.
For more information about creating script records, see Creating a Script Record.
To create the script record:
-
Upload your script to the NetSuite File Cabinet.
-
Go to Customization > Scripting > Scripts > New.
-
Select your script from the Script File list and click Create Script Record. The Script page is displayed.
-
On the Script page, enter the following values:
Field
Value
Name
Preferred Posting Period
ID
_cs_preferred_posting_period
NetSuite prepends ‘customscript’ to this ID.
Description
This script allows the user to select a preferred posting period that is then copied into the standard posting period field.
-
Optionally set any other fields on the script record as desired.
-
Click Save and Deploy. The Script Deployment page appears. Continue with Step 4: Deploy the Script.
Step 4: Deploy the Script
After you create the script record for your script, you can create a script deployment record for it. A script deployment record determines how, when, and for whom the script runs.
For more information about script deployment records, see Script Deployment.
To deploy the script:
-
Complete the steps in Step 3: Create the Script Record.
-
On the Script Deployment page, enter the following values:
Field
Value
Applies To
Invoice
ID
_cs_preferred_posting_period
NetSuite prepends 'customdeploy' to this ID.
Status
Testing
The Testing status allows the script owner to test the script without affecting other users in the account.
Log Level
Debug
The Debug level will write all
log.debug
statements in the script to the Execution Log tab of the script deployment record as well as all errors.Audience > Roles
Check Select All
-
Click Save.
Step 5: Test the Solution
After you create the script record and deploy your script, you can test your solution by creating an invoice and confirming that the current posting period shows in the Preferred Posting Period field is copied to the standard Posting Period field.
To test your solution:
-
Go to Transactions > Sales > Create Invoices to create a new invoice.
-
In the Custom Form field, select Preferred Posting Invoice.
-
Confirm the following results:
-
The Preferred Posting Period field shows on the top of the form
-
The most recent open period from your saved search results displays in the Preferred Posting Period field
-
The most recent open period from your saved search results is also copied in the standard Posting Period field (if you chose to leave it visible on the form)
For example:
Note:Your Invoice page may be slightly different from the example above depending on how you created your customized Preferred Posting Invoice. That is, you may have set fields differently than what was done in this tutorial for your form may look different.
-
Related Topics
- SuiteCloud Customization Tutorials
- Add Custom Button to Execute a Suitelet
- Calculate Commission on Sales Orders
- Copy a Value to the Item Column
- Disable Tax Fields
- Hide a Column in a Sublist
- Populate Item Substitution
- Set Purchase Order Exchange Rate
- Set the Item Amount to Zero for Marketing Orders
- Set Default Values in a Sublist
- Track Customer Deposit Balances
- Validate Order on Entry