/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/search', 'N/record'], function(search, record) {
function beforeSubmit(context) {
var newRecord = context.newRecord;
// Ensure this script only runs on edit
if (context.type !== context.UserEventType.EDIT) {
return;
}
// Get the record type and internal ID dynamically
var recordType = newRecord.type;
var recordId = newRecord.id;
if (!recordId) {
return;
}
// Create the search to find the latest system note for this record
var systemNoteSearch = search.create({
type: recordType, // Use dynamic record type
filters: [
["type", "anyof", recordType], // Dynamic record type filter
"AND",
["internalid", "anyof", recordId],
"AND",
["systemnotes.type", "is", "F"], // Filter for field changes
"AND",
["mainline", "is", "T"]
],
columns: [
search.createColumn({
name: "date",
join: "systemNotes",
label: "Date",
sort: search.Sort.DESC // Sort by date descending to get the latest
}),
search.createColumn({
name: "oldvalue",
join: "systemNotes",
label: "Old Value"
}),
search.createColumn({
name: "newvalue",
join: "systemNotes",
label: "New Value"
})
]
});
// Run the search and get the first result
var searchResult = systemNoteSearch.run().getRange({
start: 0,
end: 1
});
if (searchResult && searchResult.length > 0) {
var systemNoteDate = searchResult[0].getValue({
name: "date",
join: "systemNotes"
});
var lastModifiedDate = newRecord.getValue('lastmodifieddate');
// Compare the system note date with the last modified date
if (systemNoteDate === lastModifiedDate) {
log.debug('Record has been edited', 'Record ID: ' + recordId);
// Additional logic when the record has been edited
} else {
log.debug('No edit detected', 'Record ID: ' + recordId);
}
} else {
log.debug('No system notes found', 'Record ID: ' + recordId);
}
}
return {
beforeSubmit: beforeSubmit
};
});