将一个添加到搜索

时间:2018-01-11 18:03:05

标签: javascript netsuite suitescript

当序列号不存在时,什么都不做,当序列号不存在时,搜索我们的数据库并添加一个到找到的最高序列号。我的代码没有添加一个。所以我只是保持相同的数字。  当序列号不存在时,什么都不做,当序列号不存在时,搜索我们的数据库并添加一个到找到的最高序列号。我的代码没有添加一个。所以我只是保持相同的数字。

 // Is there a current serial number? If yes, use that sequence number else generate a new one.
        var currentSerialNumber = nlapiGetFieldValue('custrecordbcs_arm_serial');
        if (currentSerialNumber == null || currentSerialNumber == '')
            {
            nlapiLogExecution('DEBUG','NUmber of REcords3','here');
            // ok - serial number is blank - let'g get a new sequence number
                var SerialNo = nextSerialNo();

            } else {
                nlapiLogExecution('DEBUG','NUmber of REcords4','here');
                // a serial number has already been issued - use it's sequence number
                var SerialNo = currentSerialNumber.substring(10,14);

            }




        var modulecode = 'ARM';
        var tmpSubscription = nlapiGetFieldValue('custrecordbcs_arm_c');
        if (tmpSubscription == 'T') {
            var subscription = true;
        } else {
            var subscription = false;
        }
        if (subscription == true) {
          isSubscription = true;
          serialtype = 'C';
        } else {
          isSubscription = false;
        }
        // calculating the final serial number 
        var finalserialnumber = (serialtype + HashKey + months + year + modulecode + SerialNo);
        nlapiLogExecution('DEBUG','Final Serial',finalserialnumber);
        // calculating the unlock key
        var unlockkey = Getunlockforuser(companyname, finalserialnumber, isSubscription);
        nlapiLogExecution('DEBUG','Unlock',unlockkey);
        // this is placing the serial number and unlock key into the fields in netsuite.
        nlapiSetFieldValue('custrecordbcs_arm_unlock',unlockkey);
        nlapiSetFieldValue('custrecordbcs_arm_serial',finalserialnumber);
        //

        }




        function nextSerialNo(){
            // this function will calculate the serial number based off the highest record found in netsuite
            nlapiLogExecution('DEBUG','NUmber of REcords1','here');
            var filters = new Array();
            filters[0] = new nlobjSearchFilter('isinactive',null,'is','F');
            filters[1] = new nlobjSearchFilter('custrecord_bcs_cm_lapse',null,'is','F');
            filters[2] = new nlobjSearchFilter('custrecordbcs_arm_serial',null,'isnotempty','F');

            var columns = new Array();
            columns[0] = new nlobjSearchColumn('internalid');
            columns[1] = new nlobjSearchColumn('custrecordbcs_arm_serial');


            var highNumber = 0;
            var allUnlockKeys = nlapiSearchRecord('customrecord_bcs_contracts',null,filters,columns);


            nlapiLogExecution('DEBUG','NUmber of REcords','null');

            for (var i=0; allUnlockKeys != null && i < allUnlockKeys.length; i++)
            {

                var x  = Number(allUnlockKeys[i].getValue(columns[1]).substring(10,14));
                nlapiLogExecution('DEBUG','value of x',x.toString());
                if (x > highNumber){
                        highNumber = x+1;
                 }
             }

                return padstring(highNumber,4);
          }

1 个答案:

答案 0 :(得分:0)

首先,我建议转到SuiteScript 2.0。有人说,这是一个快速而肮脏的尝试来回答你的问题,因为我不能100%确定你的数据是什么样的。我还将数字解析为整数,以确保它是一个数字。我还会在生产版本中添加一些测试。希望这有助于指明您正确的方向。

简而言之,使用您的搜索,但按您要添加的字段降序排序(因此您的第一个结果应该是当前最高的)。您也可以在已保存的搜索中执行此操作,只需加载该搜索,只查看第一行。

require(['N/search'],function(search){
  function nextSerialNo(){
    log.debug('NUmber of REcords1','here');
    var highNumber=0;
    var resultArray=[];
    var contractlineSearchObj=search.create({
      type:"customrecord_bcs_contracts",
      filters:[["isinactive","is",false],"and",["custrecord_bcs_cm_lapse","is",false],"and",["custrecordbcs_arm_serial","isnotempty"]],
      columns:[
        search.createColumn({
          name:"internalid",
        }),
        search.createColumn({
          name:"custrecordbcs_arm_serial",
          sort:search.Sort.DESC
       })
      ]
    }).run().each(function(result){
      log.debug('result: ',JSON.stringify(result));
      var highNumber=result.getValue({name:'custrecordbcs_arm_serial'});
      log.debug('highNumber1: ',JSON.stringify(highNumber));

      var highNumber=highNumber.toString().substring(2,3);
      log.debug('highNumber2: ',JSON.stringify(highNumber));

      var highNumber=parseInt(highNumber)+1;
      log.debug('highNumber3: ',JSON.stringify(highNumber));
      return false
    });

    return padstring(highNumber,4);
  }

  nextSerialNo()
});

此外,这是2.0模块的主页:SuiteScript 2.0 Modules