Any idea why this is not working? Trying to change...
# suitescript
j
Any idea why this is not working? Trying to change the quantity value on line items in the fulfillment record:
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
 define(['N/record', 'N/search'],
 /**
  * @param {record} record
  * @param {search} search
  */
 function(record, search) {
    

     function beforeLoad(scriptContext) {

         var recType = record.load({type: record.Type.ITEM_FULFILLMENT, id: 5679548});
 
         var sublistRecord = recType.getSublistSubrecord({
             sublistId: 'item',
             fieldId: 'item',
             line: 0
         });
         
         //spin through the sublist of items
         var count = sublistRecord.getLineCount({sublistId: 'item'});
         for ( var i=0; i < count ; i++)
         {
             var id = sublistRecord.getSublistValue({sublistId: 'item', fieldId: 'id', line: i});
             var fulfillmentQty = sublistRecord.getSublistValue({sublistId: 'item', fieldId: 'quantity', line: i});
             var nsdrReturn = sublistRecord.getSublistText({sublistId: 'item', fieldId: 'custcol_delivery_routing_return_qty', line: i});
 
             log.debug(id + fulfillmentQty + nsdrReturn);
 
             sublistRecord.setSublistValue({sublistId: 'item', fieldId: 'quantity', line: i, value: 10});
 
         }
         
         //commit the item record.  The sublist will update
         recType.save();
     }
 
     return {
         beforeLoad: beforeLoad,
     };
     
 });
s
The simple approach to that code (with NFT).
Copy code
typescript
const fulfillment = new Itemfulfillment(5679548)
_.map(fulfillment.item,  l => l.quantity = 10)
fulfillment.save()
In general, the code you pasted suggest to read up on the difference between sublist and subrecord as well as records having
id
fields (where sublists do not)
j
I literally copied most of the code from Zigman's site
they are changing the value on a line item on Inventory Item
idk I think I just realized I can't change the qty on item fulfillments
even on the UI it overrides my changes immediately
@stalbert thank you for reviewing
s
the [inventorydetail] subrecord on item lines is usually referenced in the
inventorydetail
field. I don't have any opinion on this Zigman but that code doesn't look right to me.
b
that article looks reasonable, it treats the vendor price subrecord on the item's vendor sublist correctly
problem with the code jc is trying is that item is not a subrecord
although its probably a terrible idea to update a record during a beforeLoad user event
j
@battk I was considering a scheduled script last night. Would this be a better approach?
I'm trying to update the qty on the line item. Subtracting (quantity - column b) and setting the value in the qty column.
s
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
 define(['N/record', 'N/search'],
 /**
  * @param {record} record
  * @param {search} search
  */
 function(record, search) {
    

     function beforeLoad(scriptContext) {

         var recType = record.load({type: record.Type.ITEM_FULFILLMENT, id: 5679548,isDynamic:true});

         var count = recType.getLineCount({sublistId:'item'});

 /*
         var sublistRecord = recType.getSublistSubrecord({
             sublistId: 'item',
             fieldId: 'item',
             line: 0
         });*/
         
         //spin through the sublist of items
         //var count = sublistRecord.getLineCount({sublistId: 'item'});
         for ( var i=0; i < count ; i++)
         {
             var selectLine = recType.selectLine({sublistId:'item',line:i});
             var id = recType.getCurrentSublistValue({sublistId:'item',fieldId:'id'});
             //var id = sublistRecord.getSublistValue({sublistId: 'item', fieldId: 'id', line: i});
             var fulfillmentQty =  recType.getCurrentSublistValue({sublistId:'item',fieldId:'quantity'});
            // var fulfillmentQty = sublistRecord.getSublistValue({sublistId: 'item', fieldId: 'quantity', line: i});
             var nsdrReturn  = recType.getCurrentSublistText({sublistId:'item',fieldId:'custcol_delivery_routing_return_qty'});
             //var nsdrReturn = sublistRecord.getSublistText({sublistId: 'item', fieldId: 'custcol_delivery_routing_return_qty', line: i});
 
             log.debug(id + fulfillmentQty + nsdrReturn);
             recType.setCurrentSublistValue({sublistId:'item',fieldId:'quantity',value:10});
             //sublistRecord.setSublistValue({sublistId: 'item', fieldId: 'quantity', line: i, value: 10});
             recType.commitLine({sublistId:'item'});
         }
         
         //commit the item record.  The sublist will update
         recType.save();
     }
 
     return {
         beforeLoad: beforeLoad,
     };
     
 });
not sure if it's style but I almost always work this way with sublists. The moment I consider i set value on the sublist, instant dynamic mode not looking back for me honestly working non-dynamically has only two reasons - a) you're not changing sublists, b) you're loading to change some stuff... (but even for stuff passed to N/render I would work in dynamic mode...
s
Just the opposite for me. I find using standard mode to be better for all cases and only use dynamic mode if I must. also, while there are some actions that require dynamic mode, I've also run into situations where dynamic mode did not work and standard mode was the solution.
1
☝️ 1
j
@Sciuridae54696d thank you!
s
@JC cant say this is best answer but hiopefully its working answer 🙏
j
@Sciuridae54696d this actually worked. Thank you so much!