Client script to prevent creation of duplicate manufacturer records (custom record type)
Example: We have an existing manufacturer record “TE Connectors Inc.” and somebody tries to create “TE Connectors” this should present a warning, showing the current record and then a confirmation if they would like to proceed and create the record. Same if someone tries to create T.E. Connectors, etc.
define(['N/record', 'N/search', 'N/ui/dialog', 'N/ui/message'], function (record, search, dialog, message) {
var obj = {};
/**
* @description Function to remove special characters
* @param {*} str
* @returns
*/
function removeSpecialCharacters(str) {
return str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}
/**
* Function to check duplicate manufacturer
* @param {*} currentRecord
* @returns
*/
function checkForDuplicateManufacturers(currentRecord) {
try {
var catalogItemsObj = {};
var manufacturerName = currentRecord.getValue({ fieldId: 'name' });
var normalizedName = removeSpecialCharacters(manufacturerName);
var recordId = currentRecord.id; // Get the current record ID
// Define the search filters, excluding the current record if in edit mode
var filters = [
['isinactive', 'is', 'F']
];
// Exclude the current record from the search results if in edit mode
if (recordId) {
filters.push('AND', ['internalid', 'noneof', recordId]);
}
var manufacturerSearch = search.create({
type: 'customrecord_mhi_ibs_manufacturers',
columns: ['name'],
filters: filters
});
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 if the existing normalized name matches the current name
if (
normalizedExistingName === normalizedName ||
(normalizedExistingName.length > 2 && normalizedExistingName === normalizedName.slice(0, normalizedExistingName.length)) ||
(normalizedExistingName.length > 2 && normalizedName === normalizedExistingName.slice(0, normalizedName.length))
) {
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@checkForDuplicateManufacturers", e);
}
}
var finalResult = false;
var finalResultSet = false;
function saveRecord(context) {
try {
var currentRecord = context.currentRecord;
var recordId = currentRecord.id;
// Run duplicate check regardless of mode (create or edit)
var object = checkForDuplicateManufacturers(currentRecord);
// If duplicates are found
if (object.duplicateFound) {
if (!finalResultSet) {
dialog.confirm({
title: 'Duplicate Manufacturer Warning',
message: 'Manufacturers with similar names already exist: ' + object.duplicateNames + '. Do you want to proceed?'
}).then(function (result) {
success(result, currentRecord);
}).catch(fail);
return false; // Prevent saving until user responds
} else {
finalResultSet = false;
return finalResult; // Allow save based on user input
}
}
return true; // Allow save if no duplicates are found
} catch (e) {
log.error("error@saveRecord", e);
return false; // Prevent save on error
}
}
function success(result, currentRecord) {
finalResult = result;
finalResultSet = true;
if (result) {
currentRecord.setValue({
fieldId: 'custrecord_dup_warn_ack',
value: true
});
getNLMultiButtonByName('multibutton_submitter').onMainButtonClick(this);
}
}
function fail(reason) {
return false;
}
return {
saveRecord: saveRecord
};
});