Function to check duplicate manufacturer records

Function to check duplicate manufacturer records in NetSuite.

function checkForDuplicateManufacturers(currentRecord) {
try{
        var catalogItemsObj = {};
        var manufacturerName = currentRecord.getValue({ fieldId: 'name' });
        var normalizedName = removeSpecialCharacters(manufacturerName);
        var manufacturerSearch = search.create({
            type: 'customrecord_mhi_ibs_manufacturers',
            columns: ['name'],
            filters: [
                ['isinactive', 'is', 'F']
            ]
        });
        var resultSet = manufacturerSearch.run();
        var currentRange = resultSet.getRange({
            start: 0,
            end: 1000
        });
        var i = 0;
        var j = 0;
        var matchingNames = [];


        while (j < currentRange.length) {
            var result = currentRange[j];
            var existingName = result.getValue({ name: 'name' });
            var normalizedExistingName = removeSpecialCharacters(existingName);


            // Check for exact matches or if one is a prefix/suffix of the other, but only consider matches that cover full words
            if (
                normalizedExistingName === normalizedName || // Exact match
                (normalizedExistingName.length > 2 && normalizedExistingName === normalizedName.slice(0, normalizedExistingName.length)) || // New name starts with existing name
                (normalizedExistingName.length > 2 && normalizedName === normalizedExistingName.slice(0, normalizedName.length)) // Existing name starts with new name
            ) {
                matchingNames.push(existingName);
            }


            i++; j++;
            if (j == 1000) {
                j = 0;
                currentRange = resultSet.getRange({
                    start: i,
                    end: i + 1000
                });
            }
        }


        if (matchingNames.length > 0) {
            catalogItemsObj.duplicateFound = true;
            catalogItemsObj.duplicateNames = matchingNames.join(', ');
        }


        return catalogItemsObj;
      }
      catch(e){
        log.error("error@checkDuplicate", e)
      }
    }

Leave a comment

Your email address will not be published. Required fields are marked *