1

I'm trying to create an inventory adjustment from a loaded saved search. We have a placeholder item that goes into negative quantity and it needs to be adjusted back up a lot, so I'm attempting to automate the process. The code below so far works, but I don't know where to put the loop for creating the inventory adjustment lines. And did I put the loop for getting the saved search values in the right place? Thanks.

      if(resultRange) {
      var newAdjust = record.create({
      type:record.Type.INVENTORY_ADJUSTMENT,
      isDynamic:true});

        newAdjust.setValue({
        fieldId: 'account',
            value: 288,
        });

        newAdjust.setValue({
            fieldId: 'customform',
            value: 142,
        })


        for(i=0;i < resultRange.length;i++) {
                        
            var adjLocation = resultRange[i].getValue({
                name : 'name',
                join : 'inventoryLocation'
            });
            var adjItem = resultRange[i].getValue('itemid');
            var adjQty = resultRange[i].getValue('locationquantityonhand') * -1;

          }

EDIT: I figured it out. This is working.

/**
 * @NApiVersion 2.0
 * @NScriptType ScheduledScript
 */
 define(['N/record', 'N/search','N/runtime'],
 /**
* @param{record} record
* @param{search} search
*/
 function(record, search, runtime) {
    function execute(context) {
        
var mySearch = search.load({
    id: 'customsearch_bom_negative_inventory'
});

 var myResultSet = mySearch.run();mySearch.run();
var resultRange = myResultSet.getRange({
            start: 0,
            end: 50
        });

    if(resultRange) {

        var newAdjust = record.create({
            type:record.Type.INVENTORY_ADJUSTMENT,
            isDynamic:true});

            newAdjust.setValue({
                fieldId: 'subsidiary',
                 value: '1'
            });

        newAdjust.setValue({
            fieldId: 'account',
             value: '288'
        });

       for(i=0;i < resultRange.length;i++) {
                        
            var adjLocation = resultRange[i].getValue({
                name : 'internalid',
                join : 'inventoryLocation'
            });
            var adjItem = resultRange[i].getValue('itemid');
            var adjQty = resultRange[i].getValue('locationquantityonhand') * -1;

            newAdjust.selectNewLine({
                sublistId: 'inventory'
            });

            newAdjust.setCurrentSublistValue({
                sublistId: 'inventory',
                fieldId: 'item',
                value: 3405
             });

             newAdjust.setCurrentSublistValue({
                sublistId: 'inventory',
                fieldId: 'location',
                value: adjLocation
             });

             newAdjust.setCurrentSublistValue({
                sublistId: 'inventory',
                fieldId: 'adjustqtyby',
                value: adjQty
             });  
             
             newAdjust.commitLine({
                sublistId: 'inventory'
             });
          
          log.debug(adjLocation);
        
        }

        newAdjust.save();

    }
}
    return {
        execute: execute
    };
    });
Mikemacx
  • 31
  • 5

0 Answers0