/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record'], function(search, record) {
/**
* @description Main function
* @param {*} context
*/
function execute(context) {
try {
LastSuccessfulLoginDateOfEmployee();
} catch (e) {
log.debug("error", e);
}
}
/**
* @description Function to get the Last Successful Login date timestamp in Employee record.
*/
function LastSuccessfulLoginDateOfEmployee() {
try{
let employeeSearchObj = search.create({
type: "employee",
filters:
[
["isinactive","is","F"],
"AND",
["loginaudittrail.status","is","T"],
"AND",
["loginaudittrail.date","within","previousoneday"],
"AND",
["internalid","anyof","189107"]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "GROUP",
label: "Internal ID"
}),
search.createColumn({
name: "date",
join: "loginAuditTrail",
summary: "MAX",
label: "Date"
})
]
});
let searchResultCount = employeeSearchObj.runPaged().count;
log.debug("employeeSearchObj result count",searchResultCount);
employeeSearchObj.run().each(function(result){
let internalid = result.getValue({name: 'internalid', summary: 'GROUP'});
let lastLoginDate = result.getValue({name: 'date', join: 'loginAuditTrail', summary: 'MAX'});
log.debug('internalid', internalid);
log.debug('lastLoginDate', lastLoginDate);
let formattedDate = getFormattedDate(lastLoginDate)
//change formateeddate into string
log.debug('formattedDate', formattedDate);
log.debug('formattedDate - type', typeof(formattedDate));
record.submitFields({
type: record.Type.EMPLOYEE,
id: internalid,
values: {
custentity62: formattedDate
}
});
return true;
});
}
catch (e) {
log.debug("error@LastSuccessfulLoginDateOfEmployee", e);
}
}
function getFormattedDate(loginDate) {
try{
const lastLoginDate = new Date(loginDate);
// Manually extract parts of the date
const day = String(lastLoginDate.getDate()).padStart(2, '0'); // Ensures two digits
log.debug('day', day);
const month = String(lastLoginDate.getMonth() + 1).padStart(2, '0'); // Get month (0-based, so add 1)
log.debug('month', month);
const year = lastLoginDate.getFullYear();
log.debug('year', year);
let hours = lastLoginDate.getHours();
log.debug('hours', hours);
const minutes = String(lastLoginDate.getMinutes()).padStart(2, '0');
log.debug('minutes', minutes);
// Determine AM/PM
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12; // Convert to 12-hour format
hours = hours ? hours : 12; // 0 becomes 12
// Combine the parts into the desired format
const formattedDate = `${day}/${month}/${year} ${hours}:${minutes} ${ampm}`;
log.debug('formattedDate', formattedDate);
return formattedDate;
}
catch (e) {
log.debug("error@getFormattedDate", e);
}
}
return {
execute: execute
};
});