若你是開發人員,接到 User 需求要修改 record,例如: transaction record 、 custom record 、主檔資料的欄位內容時,你會使用 N/record Module 中的 Record.load + Record.setValue + Record.save 組合還是 Record.submitFields?此篇用修改 NetSuite 中資料的角度討論 SuiteScript 2.x Module: N/record 中這兩種我們常用的方式、差異、用法。
N/record 是 NetSuite 中基礎的 Module ,多瞭解:
延伸閱讀姐妹 Module – N/currentRecord:
若你不知道怎麼找 ID,請參考此篇:
Table of Contents
修改 Record 說明
根據原廠文件上說明,若針對資料的修改,Record.submitFields 會比 Record.load + Record.setValue + Record.save 的組合更有效率,且使用更少 usage units。
那既然這樣為什麼還需要使用 Record.load + Record.setValue + Record.save 的組合來修改資料呢?因為使用 Record.submitFields 能修改的資料有限,例如 sublist、subrecord 上的資料沒辦法用此方法來修改。另外若修改內容後需要 submit 的動作去觸發執行其他動作,需要用 Record.save 去處理(換言之,若你要修改資料而不觸發後續程式,則可考慮使用 Record.submitFields)。
Record.load + Record.setValue + Record.save 組合 Script 說明
以下提供兩種方式的基本資訊:
| NO. | Record.load + Record.setValue + Record.save | Record.submitFields |
|---|---|---|
| 1. 使用範圍 | 全部的 Record | 主要作用在 body fields。大致上重點如下: 支援 不支援 |
| 2. Governance | Record.load Transaction records: 10 units Custom records: 2 units All other records: 5 units Record.setValue None Record.save Transaction records: 20 units Custom records: 4 units All other records: 10 units -> 所以用這個組合修改 transaction record 至少要 30 units | Transaction records: 10 units Custom records: 2 units All other records: 5 units -> 所以用它修改 transaction record 要 10 units |
| 3. Supported Script Types | Client and server scripts | Client and server scripts |
| 4. 參考文章 | Record.submitFields | |
| 5. Note | 注意若是修改 subrecord,觀念有點不同,了解更多: |
此組合可以在既有的 record (有 ID) 上進行欄位值的新增與更改,包含 record 與 sublist。使用方式可以參考以下範例:
// Governance: 30 units
var objRecord = record.load({
type: record.Type.CUSTOMER,
id: 45,
isDynamic: true,
});
objRecord.setValue{{
fieldId: 'email',
value: 'customer45@test.com'
}}
var recordId = objRecord.save();
說明
#1 type:可以使用 record.Type.CUSTOMER 或是 ‘customer’,若是 custom record,直接輸入 ID,例如 type: ‘customrecord_izg_example’。延伸閱讀,請參考這篇 record.Type
#2 id:internal ID
#3 isDynamic:若是 true,則程式的運行方式跟 UI 上的操作類似,請參考此篇
#4 setVaule: 設定想要更新的欄位與值
#5 record.load ~ record.save 間依照需求增加程式
Record.submitFields Script 說明
在符合使用條件的前提下,此方式可以在既有的 record (有 ID) 上進行欄位值的新增與更改。不需要額外做 submit(save)。使用方式可以參考以下範例:
// Governance: 10 units
var customerId = record.submitFields({
type: record.Type.CUSTOMER,
id: 45,
values: {
email: 'customer45@test.com'
}
}) 說明
#1 type:可以使用 record.Type.CUSTOMER 或是 ‘customer’,若是 custom record,直接輸入 ID,例如 type: ‘customrecord_izg_example’。延伸閱讀,請參考這篇 record.Type
#2 id:internal ID
#3 values:設定想要更新的欄位與值
結語

此篇我們用 NetSuite 中修改 record 的例子,分享了 N/record Module 中的 Record.load + Record.setValue + Record.save 組合與 Record.submitFields 這兩組修改方法與差異,以及 Script 範本。除此之外還彙整了多項小知識與功能,讓你在研究這功能上更完整,事半功倍。
看完這篇後,希望你對以下有一定程度的了解唷!(以下都在文章中有連結可參考。)
#1 record.Type 的運用方式
#2 N/Record & N/currentRecord
#3 subrecord
#4 isDynamic – dynamic mode & standard mode
歡迎跟我們交流~
專注於系統客製化、系統維運與系統優化領域的團隊。致力於讓使用者系統變好用!
NetSuite Record Export 已在Google Chrome Web Store上架,歡迎任何反饋。
合作: info@inzaghi-corp.com
