NetSuite SuiteScript2.0:套件商品成员替换脚本问题求助
Hey David, let's break down and fix both issues you're facing with your workflow action script for updating Kit/Package items.
Issue 1: Can't Use Field Tags Directly as Values
First off, the {custitem_burkett_bestmemberitem} syntax you tried is for NetSuite's FreeMarker templating (like saved searches or email templates)—it doesn't work in SuiteScript. To pull the value of that custom field, you need to use the currentRecord.getValue() method. You also had a couple of case-sensitive syntax mistakes in your API calls:
- NetSuite SuiteScript 2.0 methods use camelCase (lowercase first letter), so
SetCurrentSublistValueshould besetCurrentSublistValue - The parameter name
Valueneeds to be lowercasevalue(API parameters are strict about case)
Issue 2: "No Matching Audience" Error
This error means your workflow action script's deployment isn't configured to run on Kit/Package records. Here's how to fix it:
- Navigate to your script deployment record (
Customization > Scripting > Script Deployments) - Find your "Set Best Member Item" deployment
- In the Audience tab:
- Under Applies To, make sure
Inventory Itemis selected (Kit/Package is a sub-type of Inventory Item) - To narrow it down specifically to Kit/Package items, check the Item Subtype filter and select
Kit/Package
- Under Applies To, make sure
- Save the deployment, then re-test your workflow
Corrected Full Script
I updated your script to include missing logic for removing existing kit members, fixed all syntax issues, and properly fetched the custom field value:
define(["N/currentRecord"], function(currentRecord) { var rec = currentRecord.get(); function replaceKitMembers() { // Step 1: Remove all existing kit members from the item sublist var lineCount = rec.getLineCount({ sublistId: "item" }); // Delete lines from last to first to avoid index shifting problems for (var i = lineCount - 1; i >= 0; i--) { rec.removeLine({ sublistId: "item", line: i }); } // Step 2: Get the ID of the best member item from the custom field var bestMemberItemId = rec.getValue({ fieldId: "custitem_burkett_bestmemberitem" }); // Only proceed if we have a valid item ID if (bestMemberItemId) { // Step 3: Insert the new kit member rec.insertLine({ sublistId: "item", line: 0 // Insert at the first position }); rec.setCurrentSublistValue({ sublistId: "item", fieldId: "item", value: bestMemberItemId }); rec.setCurrentSublistValue({ sublistId: "item", fieldId: "quantity", value: 1 }); rec.commitLine({ sublistId: "item" }); } } // Run the function when the script executes replaceKitMembers(); });
Key Improvements:
- Added logic to remove all existing kit members (critical for fully replacing the kit contents)
- Properly fetches the custom field value using
currentRecord.getValue() - Fixed all case-sensitive API method/parameter names
- Added a check to avoid inserting empty/invalid items
- Deletes lines from last to first to prevent index errors when removing multiple entries
内容的提问来源于stack exchange,提问作者David

