Installation and Setup > Customizing ServiceMax > Custom JavaScript Snippets Support > Sample Code Snippets > Scenario 3: To set/ update the End DateTime entry using the Start DateTime
Scenario 3: To set/ update the End DateTime entry using the Start DateTime
A technician adds the Start Datetime field in the detail line of the Work Order. The End Datetime field is updated using the custom code snippet.
Sample Snippet
This sample code snippet takes the start date time of a labor line and sets the end date time on the labor line and header by adding 15 minutes to the start date time. The snippet is configured on the On Add Record event or On change event of the Start DateTime field of Work Detail in a typical debrief SFM.
$event.get(function(response) {
var parsedRec = JSON.parse(response);
var currentRecordIndex = parsedRec.currentRecIndex;
var sectionId = parsedRec.currentSectionId;
var currentRecordId = parsedRec.currentRec.Id;
var startTimeSelected = parsedRec.currentRec[`${SVMX.OrgNamespace}__Start_Date_and_Time__c`];
if (startTimeSelected && startTimeSelected.length > 0 && !isNaN(Date.parse(startTimeSelected.replace(/-/g, '/')))) {
var startDateTime = new Date(startTimeSelected.replace(/-/g, '/'));
const minutes = startDateTime.getMinutes();
startDateTime.setMinutes(minutes + 15);
var endDateTimeString = startDateTime.getFullYear() + "-" +
(startDateTime.getMonth() + 1 < 10 ? '0' + (startDateTime.getMonth() + 1) : startDateTime.getMonth() + 1) +
"-" + (startDateTime.getDate() < 10 ? '0' + startDateTime.getDate() : startDateTime.getDate()) +
" " + (startDateTime.getHours() < 10 ? '0' + startDateTime.getHours() : startDateTime.getHours()) + ":" +
(startDateTime.getMinutes() < 10 ? '0' + startDateTime.getMinutes() : startDateTime.getMinutes()) + ":00";
console.log(endDateTimeString);
$sfm_records.get(sfmData => {
if (sfmData) {
var request = {};
sfmData = JSON.parse(sfmData);
var header = sfmData.header;
var details = sfmData.details;
request.header = request.header || {};
if (header && header.Id === currentRecordId) {
request.header['fields'] = [{
'name': 'SVMXC__End_Date_and_Time__c',
'value': endDateTimeString
}];
}
if (details && currentRecordIndex !== -1) {
var detailKeys = Object.keys(details);

if (sectionId) {
request.details = request.details || {};
request.details[sectionId] = request.details[sectionId] || [];
request.details[sectionId].push({
'index': currentRecordIndex,
'fields': [{
'name': 'SVMXC__End_Date_and_Time__c',
'value': endDateTimeString
}]
});
}
}
$sfm_records.setFieldValue(request, result => {
console.log(JSON.parse(result));
$response({
'status': 'success',
'error_message': ''
});
});
} else {
$response({
'status': 'error',
'error_message': 'Invalid response from $sfm_records.get API'
});
}
});
} else {
$response({
'status': 'error',
'error_message': 'Invalid/Empty Date value'
});
}
});
Was this helpful?