I'm getting an error when trying to update a line ...
# suitescript
j
I'm getting an error when trying to update a line level field on an item fulfillment. I'm super new to scripting and would appreciate any insight. Here is the error I keep getting:{"type":"error.SuiteScriptError","name":"SSS_INVALID_SUBLIST_OPERATION","message":"You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.","id":"","stack":["anonymous(N/serverRecordService)","anonymous(/SuiteScripts/producttype item ful.js:75)","each(N/searchObject)","execute(/SuiteScripts/producttype item ful.js:22)"],"cause":{"type":"internal error","code":"SSS_INVALID_SUBLIST_OPERATION","details":"You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.","userEvent":null,"stackTrace":["anonymous(N/serverRecordService)","anonymous(/SuiteScripts/producttype item ful.js:75)","each(N/searchObject)","execute(/SuiteScripts/producttype item ful.js:22)"],"notifyOff":false},"notifyOff":false,"userFacing":false}; ID: Basically, I have a saved search showing lines on item fulfillments that are missing a CLASS and I would like to update that field from the item record so our financials are split by class more accurately. Since I can't adjust via import, I thought a script would be able to do it.
e
Can you share the relevant code as well?
j
The whole thing, or just around line 75
// Set class on the line level itemFulfillmentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'class', value: classFromItem });
e
You'll probably need to share more. If it's a lot of code, you can use Slack's snippet to format and highlight it
j
I'll put the whole thing here, I don't think it is too much: /** * @NApiVersion 2.0 * @NScriptType ScheduledScript * @NModuleScope SameAccount */ define(['N/search', 'N/record'], function(search, record) { function execute(context) { // Define saved search ID for item fulfillments with missing class on the line level var savedSearchId = 'customsearch8944'; // Load saved search var mySearch = search.load({ id: savedSearchId }); // Run search var resultSet = mySearch.run(); // Process search results resultSet.each(function(result) { // Retrieve item fulfillment record ID var itemFulfillmentId = result.getValue({ name: 'internalid' // Assuming internal ID field is named 'internalid' }); // Load item fulfillment record var itemFulfillmentRecord = record.load({ type: record.Type.ITEM_FULFILLMENT, id: itemFulfillmentId, isDynamic: true }); // Loop through line items var lineCount = itemFulfillmentRecord.getLineCount({ sublistId: 'item' }); for (var i = 0; i < lineCount; i++) { // Get class from the item var itemClass = itemFulfillmentRecord.getSublistValue({ sublistId: 'item', fieldId: 'class', line: i }); // Check if class is missing if (!itemClass) { // Get class from the item record var itemId = itemFulfillmentRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }); var item = record.load({ type: record.Type.INVENTORY_ITEM, // Assuming the item is an inventory item id: itemId, isDynamic: false }); var classFromItem = item.getValue({ fieldId: 'class' }); // Debugging: Log classFromItem value if (classFromItem) { log.debug('classFromItem:', classFromItem); } else { log.debug('classFromItem is empty or undefined'); } // Debugging: Log before setting sublist value log.debug('Before setting sublist value'); // Set class on the line level itemFulfillmentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'class', value: classFromItem }); // Debugging: Log after setting sublist value log.debug('After setting sublist value'); } } // Save changes to the item fulfillment record var recordId = itemFulfillmentRecord.save(); return true; // Continue processing search results }); } return { execute: execute }; });
The saved search is limited to 1 IF and it only has Inventory items on it. The missing line detail is entered correctly on the item record of that line.
a
Untitled.js
👍🏻 1
e
You need to use
selectLine
on your IF record before using
setCurrentSublistValue
👍 1
e
And then you will need to
commitLine()
afterward
👍 2
How many lines are on a typical IF?
j
That need to be updated via this script, only 1-5 on one IF, but mostly just 1. We have a previous customization that clears out the first line attributes like CLASS, so I am trying to come behind and get them updated.
e
Instead of loading the Item record, which is an expensive operation, you might want to retrieve the
class
using search.lookupFields()
j
Ideally, a workflow catches it on the SO first, but there are a few that make through that we need updatd
Thank you! I'll read up on this and attempt those changes!
e
A while ago I wrote an article about doing lookups here
a
also internal id is a special case... instead of this
Copy code
// Retrieve item fulfillment record ID
            var itemFulfillmentId = result.getValue({
                name: 'internalid' // Assuming internal ID field is named 'internalid'
            });
you can just do this
Copy code
var itemFulfillmentId = result.id;
or you can just not assign it to a variable at all and just use
result.id
as is
... yours still works obviously but this is just cleaner
🙏 1
e
Welcome to the SuiteScript world, btw 🙂
j
It's a big one! I'm excited to get into, even for some simple things!
🙌🏻 2
🙌 1