does anyone know if you can add values to a custom...
# suitescript
n
does anyone know if you can add values to a custom sublist (scripted) in a ClientScript? @battk
b
some sublists yes, some no, you have not shared enough information to tell
n
Copy code
/*******************************************************************
*
* Name: CITR_CS_AddInventoryCountSublist.js
* @NApiVersion 2.1
* @NScriptType ClientScript
* Version 0.0.1
*
*
* Author: Nicolas Bean
* Purpose: Adding inventory count sublist to inventory count record
* Script: CITR_CS_AddInventoryCountSublist.js
* Deploy: CITR_CS_AddInventoryCountSublist
*
*
* ******************************************************************* */

define(['N/record', 'N/log', 'N/runtime', 'N/search'], function(record, log, runtime, search) {

  const sublistId = 'custpage_inventorycountsublist';
  const subtabId = 'custpage_inventorycount';

  function fieldChanged(context) {
    log.debug('context', context);
    var currentRecord = context.currentRecord;
    var currentSublist = context.sublistId;
    var currentField = context.fieldId;
    var currentUser = runtime.getCurrentUser();
    var userSubsidiary = currentUser.subsidiary;

    if (currentField == 'location') {
      try {

        var currentLocation = currentRecord.getValue({fieldId: 'location'});
        var items = findNetSuiteItemQty(userSubsidiary, currentLocation);
        log.debug('items', items);
        log.debug('items.length', items.length);

        var customsublist = currentRecord.getSublist({ sublistId: sublistId});
        log.debug('customsublist', customsublist);

        for (var i = 0; i < items.length; i++) {

          customsublist.selectNewLine({
            sublistId: sublistId
          })

          customsublist.setCurrentSublistValue({
            id: 'inventorycount_item',
            line: i,
            value: items[i].internalid
          });
          customsublist.setCurrentSublistValue({
            id: 'inventorycount_itemdescription',
            line: i,
            value: items[i].displayname
          });
          customsublist.setCurrentSublistValue({
            id: 'inventorycount_uom',
            line: i,
            value: items[i].uom
          });
          customsublist.setCurrentSublistValue({
            id: 'inventorycount_avgcost',
            line: i,
            value: items[i].locationaveragecost
          });
          customsublist.setCurrentSublistValue({
            id: 'inventorycount_netsuiteqty',
            line: i,
            value: items[i].locationquantityonhand
          });

          customsublist.commitLine({
            sublistId: sublistId,
            ignoreRecalc: true
          });
        }

    } catch (e) {
      log.debug('Error reads: ', e.name + e.message);
    }
    }
    }


        function findNetSuiteItemQty(subsidiary, location) {

          var response = [];

          var inventoryitemSearchObj = search.create({
            type: "inventoryitem",
            filters:
            [
               ["type","anyof","InvtPart"], 
               "AND", 
               ["subsidiary","anyof", subsidiary], 
               "AND", 
               ["inventorylocation","anyof",location], 
               "AND", 
               ["locationquantityonhand","notequalto","0"], 
               "AND", 
               ["locationquantityonhand","isnotempty",""]
            ],
            columns:
            [
               search.createColumn({name: "internalid", label: "Internal ID"}),
               search.createColumn({
                  name: "itemid",
                  sort: search.Sort.ASC,
                  label: "Name"
               }),
               search.createColumn({name: "displayname", label: "Display Name"}),
               search.createColumn({
                name: "formulanumeric",
                formula: "CASE WHEN {locationaveragecost} IS NULL THEN 0 ELSE {locationaveragecost} END",
                label: "Formula (Numeric)"
             }),               search.createColumn({name: "inventorylocation", label: "Inventory Location"}),
               search.createColumn({name: "locationquantityonhand", label: "Location On Hand"}),
               search.createColumn({name: "custitem_item_unit", label: "Unit"})
            ]
         });
         let searchResultData = inventoryitemSearchObj.runPaged({pageSize: 1000});
         searchResultData.pageRanges.forEach(function (pageRange) {
          let resultPage = searchResultData.fetch({index: pageRange.index});
          resultPage.data.forEach(function (result) {

            let columns = result.columns;
            let item = {};

            item.internalid = result.getValue(columns[0]);
            item.name = result.getValue(columns[1]);
            item.displayname = result.getValue(columns[2]);
            item.locationaveragecost = result.getValue(columns[3]);
            item.inventorylocation = result.getValue(columns[4]);
            item.locationquantityonhand = result.getValue(columns[5]);
            item.uom = result.getValue(columns[6]);
            response.push(item);

          });

        });

        return response;

        }

        return {
          fieldChanged: fieldChanged
        };

});
here is the script
i also tried sublist type = inlineeditor
right now its type = static
b
while you are adding the sublist and field, pay attention to the documentation on the ids of Form.addSublist, you really dont want to confuse suitescript with your ids
else you want to look at the methods of a currentRecord.Sublist
you cannot treat the Sublist the same as a currentRecord.CurrentRecord
in general, while adding sublist lines in client script, you need to use forceSyncSourcing
n
ok
how do you even add new lines in a sublist - i tried selectLine, selectNewLine none of these seem to work
b
you need the correct sublist type
and you need to learn the difference between a Sublist and a CurrentRecord
🙏 1
😂 1
n
how do you get a custom sublist with custom fields on it to retain its value when you save?
b
you would need to store the values elsewhere
and rebuild the sublist as necessary
n
right
sounds like a pain lol