Can someone porivde me sample suitescript to remov...
# suitescript
v
Can someone porivde me sample suitescript to remove a specific item and replace with itemtype itemgroup on the saleorder, I am trying to do it with a userevent script event type edit but getting error and unable to set the itemgroup type item?
a
my example of this is massively more complicated than a simple use case, so probably better if share you non working code I can debug it for you.
j
what is the error you are getting?
agree that we need to see your script
v
script /** * @NApiVersion 2.x * @NScriptType UserEventScript */ define(['N/record', 'N/search', 'N/log'], function (record, search, log) { function afterSubmit(context) { if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) { return; } var salesOrder; try { // Load the record in dynamic mode salesOrder = record.load({ type: record.Type.SALES_ORDER, id: context.newRecord.id, isDynamic: true }); } catch (e) { log.error('Error Loading Record', 'Failed to load Sales Order in dynamic mode: ' + e.message); return; } // Log initial line count for debugging var initialLineCount = salesOrder.getLineCount({ sublistId: 'item' }); log.debug('Initial Line Count', 'Number of item lines before processing: ' + initialLineCount); if (initialLineCount === 0) { log.error('No Item Lines', 'Sales Order has no item lines. Cannot process Item Group replacement.'); return; } for (var i = 0; i < initialLineCount; i++) { try { // Select the line in dynamic mode salesOrder.selectLine({ sublistId: 'item', line: i }); var itemText = salesOrder.getCurrentSublistText({ sublistId: 'item', fieldId: 'item' }); var description = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'description' }); // Log line details for debugging log.debug('Processing Line ' + i, { itemText: itemText, description: description }); // Check for "Amazon Test" if (itemText === 'Amazon Test' ) { // Get existing field values var rate = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'rate' }); var amount = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'amount' }); var etailOrderLineId = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'custcol_celigo_etail_order_line_id' }); // Replace the line with an Item Group (hardcoded ID 25005) var newItemId = 17698; if (newItemId) { // Set the Item Group salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: newItemId }); // Set quantity (use extracted quantity or default to 2) salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: quantity || 2 }); // Set custom price if applicable if (rate != null || amount != null) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'price', value: -1 // Custom price level }); } if (rate != null) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'rate', value: rate }); } if (amount != null) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: amount }); } else { // Ensure amount is set to avoid invalid lines var calculatedAmount = rate && quantity ? rate * (quantity || 2) : 0; salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: calculatedAmount }); } if (etailOrderLineId) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'custcol_celigo_etail_order_line_id', value: etailOrderLineId }); } // Commit the Item Group line salesOrder.commitLine({ sublistId: 'item' }); // Optional: Loop through component items to set custom fields var newLineCount = salesOrder.getLineCount({ sublistId: 'item' }); for (var j = i + 1; j < newLineCount; j++) { salesOrder.selectLine({ sublistId: 'item', line: j }); var itemType = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'itemtype' }); if (itemType === 'Group' || itemType === 'EndGroup') { continue; // Skip Group or EndGroup lines } // Set custom fields on component items if (etailOrderLineId) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'custcol_celigo_etail_order_line_id', value: etailOrderLineId }); } // Ensure component lines have valid pricing var compRate = salesOrder.getCurrentSublistValue({ sublistId: 'item', fieldId: 'rate' }); if (!compRate) { salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'price', value: -1 // Custom price level }); salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'rate', value: 0 // Default to 0 if no rate }); } salesOrder.commitLine({ sublistId: 'item' }); } // Log the update log.debug('Line updated with Item Group', { line: i, item: itemNameToFind, newItemId: newItemId, quantity: quantity || 2, rate: rate, amount: amount, etailOrderLineId: etailOrderLineId }); // Adjust loop index to account for added component lines i = newLineCount - 1; } else { log.error('Item Group Not Found', 'No Item Group found with ID: ' + newItemId); } } else { log.debug('No valid pattern', 'Line ' + i + ' description "' + description + '" did not match expected pattern.'); } } catch (e) { log.error('Error Processing Line ' + i, e.message); } } // Log final line count before saving var finalLineCount = salesOrder.getLineCount({ sublistId: 'item' }); log.debug('Final Line Count', 'Number of item lines after processing: ' + finalLineCount); if (finalLineCount === 0) { log.error('No Valid Lines', 'Sales Order has no valid item lines after processing. Cannot save.'); return; } // Save the record try { salesOrder.save({ ignoreMandatoryFields: false }); log.debug('Sales Order Saved', 'Successfully saved Sales Order ID: ' + context.newRecord.id); } catch (e) { log.error('Error Saving Sales Order', 'Failed to save Sales Order: ' + e.message); if (e.name === 'RCRD_HAS_BEEN_CHANGED') { log.error('Concurrency Issue', 'Record was modified by another process. Consider retrying or checking for conflicting scripts/workflows.'); } } } return { afterSubmit: afterSubmit }; });
@Anthony OConnor Can you share your example I will take a look
a
I'm not going to share it, it will just confuse you, I'm not replacing a specific item with an item group... I'm dynamically searching for all items to see if they're components of an item group, and checking if all such components are listed on other lines.... there's places with 3 nested loops deep.... it WILL NOT HELP YOU
so I think the crux of your issue is you can't edit an existing item line to have an itemgroup id item groups are multiple lines and you're editing a single line which I think is causing the issue
j
you need to remove the item then after that add in the item group
☝️ 1
not edit the item id
a
when you identify the itemid you want to change instead do a
salesOrder.removeLine
and then do a
salesOrder.selectNewLine
set the line fields and commit line
💯 1