To restrict duplicate records while saving records using a Suitelet script in NetSuite, utilize a saved search to check whether a similar record already exists in the system.
- In NetSuite, create a saved search that filters records based on the criteria to identify duplicates. This could include one or more fields that must be unique. For example: Name, Email Id or any other field.
- In Suitelet script, when handling the request to save a new record, retrieve the relevant data from the incoming request or form submission.
- Use the saved search created to check for existing records that match the specified criteria. Do this by loading the saved search and setting search filters based on the incoming data.
- Run the saved search and check if any records are returned. If records are found, restrict it from saving to NetSuite account. Display a warning message to the user, notifying them that a similar record already exists, and they may want to review before proceeding.
For Example:
Using Email address to check whether Customer record exists or not.
let email = context.request.parameters.email;
let dupSearch = search.create({
type: ‘customer’,
filters: [[’email’, ‘is’, email]]
});
let dupResults = dupSearch.run().getRange({ start: 0, end: 1 });
if (dupResults.length > 0) {
scriptContext.response.write(‘Duplicate record found. Save operation aborted.’);
} else {
// Apply the logic to save the record
scriptContext.response.write(‘Record saved successfully.’);
}