NetSuite客户端脚本:日记账分录自动填充上一行类别与部门
Hey Mark, great question! I’ve tackled this exact NetSuite scenario before, so let me break down how to build this client-side automation step by step.
How to Auto-Populate Class/Department from Previous Journal Entry Line
The key here is using a SuiteScript 2.x Client Script tied to the Journal Entry record, which listens for changes to the Account field in the line sublist and pulls values from the prior line.
Step 1: Write the Client Script
This script will trigger whenever you tab out of the Account field in a new journal line, check the previous line’s Class and Department values, and auto-fill them into the current line.
/** * @NApiVersion 2.x * @NScriptType ClientScript * @NModuleScope SameAccount */ define(['N/currentRecord'], function(currentRecord) { function fieldChanged(context) { const rec = context.currentRecord; const sublist = context.sublistId; const field = context.fieldId; const currentLineNum = context.line; // Only run logic for the journal entry line sublist and Account field if (sublist === 'line' && field === 'account') { // Skip the first line (no prior line to copy from) if (currentLineNum <= 0) return; // Grab values from the immediately preceding line const prevLineNum = currentLineNum - 1; const prevClass = rec.getCurrentSublistValue({ sublistId: 'line', fieldId: 'class', line: prevLineNum }); const prevDept = rec.getCurrentSublistValue({ sublistId: 'line', fieldId: 'department', line: prevLineNum }); // Auto-fill current line if prior line had values if (prevClass) { rec.setCurrentSublistValue({ sublistId: 'line', fieldId: 'class', value: prevClass }); } if (prevDept) { rec.setCurrentSublistValue({ sublistId: 'line', fieldId: 'department', value: prevDept }); } } } return { fieldChanged: fieldChanged }; });
Step 2: Deploy the Script
- In NetSuite, go to Customization > Scripting > Scripts > New
- Select Client Script as the script type, paste the code above, and save it
- Create a deployment for the script:
- Set Applies To to Journal Entry
- Set Status to Released
- Assign appropriate roles (e.g., your finance team roles) that need this functionality
How It Works
- When you add your first line with Class=AA and Department=BB, then click Add to create a second line
- Type an Account in the second line’s Account field and press Tab
- The script detects the Account field change, grabs AA/BB from line 0, and auto-fills them into line 1’s Class and Department fields
- If the prior line has empty Class/Department values, it won’t overwrite any input you might want to add manually
内容的提问来源于stack exchange,提问作者Mark Wagner
相关产品推荐
相关产品推荐

