Javascript NetSuite费用报表数组高效

时间:2014-07-22 18:38:18

标签: javascript arrays duplicates netsuite

好吧,我需要做的是为netsuite创建一个脚本,在脚本报告中循环显示每个费用,并根据自定义记录的搜索更新税码。我需要帮助的是效率 - 通常有多次搜索相同类别的费用,并且由于我想要的分配治理,所以我将搜索调用的次数最少,而不是多次搜索同一类别。这就是我所拥有的:

/*
 * Pseudocode:
 * search minimum amount of times - do not search same category multiple times
 * for loop cycle through each expense and save category to array
 * if category is first occurrence complete search for tax code
 * else if category is a repeat set tax code based on previous search
 * 
 */



function userEventBeforeSubmit(type){

    /*if(type!='create')
    {
        return;
    }
    */

    //amount of expenses per expense report
    var expenseNum = nlapiGetLineItemCount('expense');
    var intSubsidiary = nlapiGetFieldValue('subsidiary');

    //this for loop is attempting to push the unique categories into an array leaving out duplicates 
    for(var i = 0; i < expenseNum; i++){
        var intCategory = nlapiGetLineItemValue('expense', 'category', i);
        var arr = [];
        //if category not found push into array
        if(arr.indexOf(intCategory) === -1){
            arr.push(intCategory);
        }

        //search expense tax codes record
        var arrSearchFilters = new Array();
        arrSearchFilters[0] = new nlobjSearchFilter('custrecord_cv_expensesubsidiary', null, 'anyof', intSubsidiary);
        arrSearchFilters[1] = new nlobjSearchFilter('custrecord_cv_expensecategory', null, 'anyof', arr[i]);

        //field from expense tax codes record
        var arrSearchColumns = new Array();
        arrSearchColumns[0] = new nlobjSearchColumn('custrecord_cv_taxcode');

        var arrSearchResults = nlapiSearchRecord('customrecord_cv_expensetaxcodes', null, arrSearchFilters, arrSearchColumns);

    }






        //for loop to iterate through expenses in expense report
        for(var i =1; i < expenseNum+1; i++){

            //get value category for each line
            //var intCategory = nlapiGetLineItemValue('expense', 'category', i);
            var taxAmtTemp = nlapiGetLineItemValue('expense', 'taxamount', i);

            var searchResult = arrSearchResults[0];
            //set tax code 
            var taxCode = searchResult.getValue(arrSearchColumns[0]);
            nlapiSetLineItemValue('expense', 'taxcode', i, taxCode);

            //if tax amount changes set back to original amount
            if(taxAmtTemp != nlapiGetLineItemValue('expense', 'taxamount', i)){

                nlapiSetLineItemValue('expense', 'taxamount', i, taxAmtTemp);

            }  


        }




}

2 个答案:

答案 0 :(得分:0)

在创建记录后,用户是否需要查看正确的税码(基于自定义记录搜索返回值)?

如果您担心治理问题,可以将其设置为预定脚本 - 这样分配的脚本使用就足够了。

然后,您可以将费用记录的内部ID传递给计划脚本,并在保存记录后立即执行。因此,您的用户提交事件将充当触发器,并将记录ID作为参数传递并执行预定脚本。根据队列,记录应在几分钟内更新。虽然记录的变化不会实时发生。

答案 1 :(得分:0)

您编写的系统似乎相当合理,但您可以轻松进行优化。 'anyof'过滤器可以接受一组值。因此,在第一个循环中构建类别数组。

var arrExpenseCategory = [];
for(var i = 1; i < expenseNum+1; i++){
    var intCategory = nlapiGetLineItemValue('expense', 'category', i);
    if(arrExpenseCategory.indexOf(intCategory) === -1){
        arrExpenseCategory.push(intCategory);
    }
}

然后搜索这些记录。

//search expense tax codes record
var arrSearchFilters = [
   new nlobjSearchFilter('custrecord_cv_expensesubsidiary', null, 'anyof', intSubsidiary), 
   new nlobjSearchFilter('custrecord_cv_expensecategory', null, 'anyof', expenseCategoryArr)
]

//field from expense tax codes record
var arrSearchColumns = new Array();
arrSearchColumns[0] = new nlobjSearchColumn('custrecord_cv_taxcode');
arrSearchColumns[1] = new nlobjSearchColumn('custrecord_cv_expensecategory');

然后将这些记录放在一个数组中。

var arrSearchResults = nlapiSearchRecord('customrecord_cv_expensetaxcodes', null, arrSearchFilters, arrSearchColumns);
arrExpenseCategory = [];
for(var i = 0; i < arrSearchResults.length; i++){
    arrExpenseCategory[arrSearchResults.getValue(arrSearchColumns[1])] = arrSearchResults.getValue(arrSearchColumns[0]);
}

然后使用数组填充你的字段。

for(var i =1; i < expenseNum+1; i++){
    var taxAmtTemp = nlapiGetLineItemValue('expense', 'taxamount', i);
    nlapiSetLineItemValue('expense', 'taxcode', i, arrExpenseCategory[nlapiGetLineItemValue('expense', 'category', i)]);
    //if tax amount changes set back to original amount
    if(taxAmtTemp != nlapiGetLineItemValue('expense', 'taxamount', i)){
        nlapiSetLineItemValue('expense', 'taxamount', i, taxAmtTemp);
        }  
    }

由于原始问题中没有说明,我假设这将在beforeSubmit事件上运行。它也可以在客户端beforeSave事件上正常运行。我唯一的另一个警告是nlapiSearchRecord只返回1000个结果。如果您的子公司有超过1000个customrecord_cv_expensetaxcodes记录,则此脚本将不适合您。如eliseobeltran所述,您基本上不能在预定的脚本中运行它。