JC
09/02/2023, 4:40 PM/**
* @NApiVersion 2.0
* @NScriptType userEventScript
* @NModuleScope Public
*
*/
define(['N/search','N/record'], function(search,record) {
function afterSubmit(context) {
var stLogStatus = 'afterSubmit';
try{
var stRecordType = context.newRecord.type;
if(stRecordType == 'note'){
var recUserNote = context.newRecord;
var stCustomerId = recUserNote.getValue('entity');
var dtLastNoteDate = getLatestNoteDate(stCustomerId);
setLastContactDate(stCustomerId, dtLastNoteDate)
}
if(stRecordType == 'customer'){
var stCustomerId = context.newRecord.id;
var dtLastNoteDate = getLatestNoteDate(stCustomerId);
setLastContactDate(stCustomerId, dtLastNoteDate);
}
}catch(error){
log.error(stLogStatus, error);
}
}
function setLastContactDate(stCustomerId, dtContactDate){
if(!isEmpty(dtContactDate)) // 2024-06-01 CAW
record.submitFields({
type: record.Type.CUSTOMER,
id: stCustomerId,
values: {
'custentity_last_contact_date' : dtContactDate,
}
});
}
function getLatestNoteDate(stCustomerId){
var objSearchResults = search.create({
type: "note",
filters:
[
["customer.internalid","anyof",stCustomerId]
],
columns:
[
search.createColumn({
name: "notedate",
sort: search.Sort.DESC,
label: "Date"
})
]
});
var latestDate = null;
objSearchResults.run().each(function(result){
latestDate = result.getValue('notedate');
return false;
});
if(!isEmpty(latestDate)) latestDate = new Date(latestDate);
return latestDate;
}
function isEmpty(value) {
var logTitle = 'isEmpty';
try {
if (value == null || value == '' || (!value) || value == 'undefined') {
return true;
}
return false;
} catch (error) {
log.error(logTitle, error);
}
}
return {
afterSubmit: afterSubmit
}
});
creece
09/02/2023, 5:04 PMSelcuk Dogru
09/02/2023, 6:06 PM