razer456
03/30/2022, 7:44 PMThe Usual Suspect
03/30/2022, 9:18 PMThe Usual Suspect
03/30/2022, 9:18 PMThe Usual Suspect
03/30/2022, 10:18 PMbattk
03/31/2022, 3:18 AMbattk
03/31/2022, 3:18 AMbattk
03/31/2022, 3:21 AMrazer456
03/31/2022, 5:04 AMrazer456
03/31/2022, 5:04 AMrazer456
03/31/2022, 5:12 AMbattk
03/31/2022, 5:13 AMbattk
03/31/2022, 5:13 AMrazer456
03/31/2022, 5:13 AMbattk
03/31/2022, 5:20 AMrazer456
03/31/2022, 5:26 AMdefine(["N/ui/serverWidget","N/search","N/runtime"], function (serverWidget,search,runtime) {
    function setCustomSelectField(context) {
     var currentRec=context.newRecord;
      //Get Object of the sublist
      var sublist = context.form.getSublist({
        id: "item",
      });
  //Add field into the Sublist which object is identified
    var field=sublist.addField({
        id: "custpage_skulist",
        type: serverWidget.FieldType.SELECT,
        label: "SKU List"
     
      });
     
    }
  
    return {
      beforeLoad: setCustomSelectField,
    };
  });razer456
03/31/2022, 5:27 AMrazer456
03/31/2022, 5:28 AMdefine(["N/search", "N/runtime", "N/ui/dialog"], function (
  search,
  runtime,
  dialog
) {
  function fieldChanged(context) {
    try {
      //get Current record context
      var currentRec = context.currentRecord;
      //get the field and sublist that causing the Event
      var sublistName = context.sublistId;
      var fieldId = context.fieldId;
      //Check if desire field is triggering event
      if (sublistName == "item") {
        //Check first Vendor is selected or Not
        //As Vendor is very key for search we need it for proper flow
        var vendorSelected = currentRec.getCurrentSublistValue({
          sublistId: "item",
          fieldId: "povendor",
        });
        
        var itemSelected = currentRec.getCurrentSublistValue({
          sublistId: "item",
          fieldId: "item",
          // line:0
        });
        if (itemSelected != "" && vendorSelected != "") {
          //get the list of SKU units related to the Vendor
          var customrecordSearchObj = search.create({
            type: "customrecord389",
            filters: [
              ["custrecord155", "anyof", parseInt(vendorSelected)],
              "AND",
              ["custrecord156", "anyof", parseInt(itemSelected)],
            ],
            columns: [
              search.createColumn({
                name: "name",
                sort: search.Sort.ASC,
                label: "Name",
              }),
              search.createColumn({ name: "id", label: "ID" }),
              search.createColumn({ name: "custrecord156", label: "Item" }),
            ],
          });
          var SKUSearch = searchAll(customrecordSearchObj.run());
          var skuListObj = currentRec.getSublistField({
            fieldId: "custpage_skulist",
            sublistId: "item",
            line: 0,
          });
          //     //check weather the entered entry is in SKU unit list
          var index = 0;
          // var skuInternalId=SKUSearch[i].getValue({name: "id", label: "ID"});
          skuListObj.removeSelectOption({
            value: null,
          });
          for (var i = 0; i < SKUSearch.length; i++) {
            var SKURelated = SKUSearch[i].getValue({
              name: "name",
              label: "Name",
            });
            var skuInternalId = SKUSearch[i].getValue({
              name: "id",
              label: "ID",
            });
            if (!SKURelated == "") {
              skuListObj.insertSelectOption({
                value: skuInternalId,
                text: SKURelated,
              });
            }
          }
        }
      }
      return true;
    } catch (e) {
      log.error({
        title: "Error",
        details: e,
      });
    }
    return true;
  }
  //Function for set value from Scripted field to SKU Field And Save Record
  function saveRecord(context) {
    try {
      //get Current record context
      var currentRec = context.currentRecord;
      //Get Line count Entered by user
      var length = currentRec.getLineCount({
        sublistId: "item",
      });
      for (var i = 0; i < length; i++) {
        //Set Value from scripted field to SKU field
        currentRec.selectLine({ sublistId: "item", line: i });
        var skuListValue = currentRec.getCurrentSublistValue({
          fieldId: "custpage_skulist",
          sublistId: "item",
        });
        currentRec.setCurrentSublistValue({
          fieldId: "custcol1",
          sublistId: "item",
          value: skuListValue,
        });
        currentRec.commitLine({ sublistId: "item" });
      }
      return true;
    } catch (e) {
      log.error({
        title: "Error",
        details: e,
      });
    }
    //search function
  }
  function searchAll(resultsetCurrent) {
    var allResults = [];
    var startIndex = 0;
    var RANGECOUNT = 1000;
    do {
      var pagedResults = resultsetCurrent.getRange({
        start: parseInt(startIndex),
        end: parseInt(startIndex + RANGECOUNT),
      });
      allResults = allResults.concat(pagedResults);
      var pagedResultsCount = pagedResults != null ? pagedResults.length : 0;
      startIndex += pagedResultsCount;
      var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
    } while (pagedResultsCount == RANGECOUNT);
    var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
    return allResults;
  }
  return {
    lineInit: fieldChanged,
    saveRecord: saveRecord,
  };
});razer456
03/31/2022, 5:28 AMbattk
03/31/2022, 5:40 AM