In vendor bills I am trying to find duplicates of ...
# suitescript
p
In vendor bills I am trying to find duplicates of a vendor / invoice combination when the user enters the bill number (ie. reference no.). The idea is to be alerted that there is a possible duplicate before any other data entry is done. In the below script I am getting an Unexpected Error but am not sure why. It seems to me that the issue is with the search but am not sure what it could be. Any ideas?
Copy code
/**
        *@NApiVersion 2.0
        *@NScriptType ClientScript
        */
        define(['N/record','N/search', 'N/ui/dialog'], function (r,search,u) {
            function fieldChanged(context) {
        var recCurrent = context.currentRecord;            
                if((context.fieldId == 'tranid')) {
                    var vendorinvoice = recCurrent.getValue({
                        fieldId: 'tranid'
                    });
                    var vendor = recCurrent.getValue({ fieldId: 'name'});
                    console.log(vendorinvoice);
            var sv = search.create({
            type: "vendorbill",
                columns : [
                    search.createColumn({name: "internalid", label: "Internal ID"})
                 ],

                filters : [
                    ["type","anyof","VendBill"], 
                    "AND", 
                    ["mainline","is","T"], 
                    "AND", 
                    ["name","anyof",vendor], 
                    "AND", 
                    ["tranid","is",vendorinvoice]
                 ] 
            });
            
            var pagedData = sv.runPaged({pageSize: 1000});
            
            var printOnce = 0;
            for (var i = 0; i < pagedData.pageRanges.length; i++) {
    
                // fetch the current page data
                var currentPage = pagedData.fetch(i);
                // and forEach() thru all results
                currentPage.data.forEach(function(result) {
    
                    // you have the result row. use it like this....
                    var duplicate = result.getValue('internalid');
                    console.log(duplicate);
                    if (duplicate && printOnce==0) {
                        printOnce++;
                        alert('This vendor / vendor invoice combination already exists.')
                    }
                });
    
            }

            }
        }
        return {
                    fieldChanged: fieldChanged
        };
            });
b
the usual is to pay attention to the stack trace on the thrown error
it has a row/column numbers that should help you find what is causing your problem
p
thanks, found the issue. for vendor bills need to use entity not name for the vendor.