Installation and Setup > Customizing ServiceMax > Custom JavaScript Snippets Support > Sample Code Snippets > Scenario 2: To set/ update the Start DateTime entry using the End DateTime of the previous line
Scenario 2: To set/ update the Start DateTime entry using the End DateTime of the previous line
A technician enters the End Datetime of the previous labor line in the same Work Order. The Start Datetime field will be updated using the custom code snippet.
Sample Snippet
This sample code snippet is used to set the Start DateTime field value of the most recently created Labor line, from the previous Labor line’s End DateTime field value, if there are more than 1 labor lines. The snippet takes the End Datetime of the previous labor line and enters the value in the Start DateTime of the next labor lline. The snippet is configured on the On Add Record event in a typical debrief SFM.
$sfm_records.get(sfmData => {
if (sfmData) {
sfmData = JSON.parse(sfmData);
var request = {};
var details = sfmData.details;
if (details) {
var laborSection = null,
laborSectionId = null,
detailKeys = Object.keys(details);
for (var idx = 0; idx < detailKeys.length; idx++) {
var eachDetail = details[detailKeys[idx]];
if (eachDetail.name === 'Labor') {
laborSection = eachDetail;
laborSectionId = detailKeys[idx];
break;
}
}
if (laborSection) {
if (laborSection.lines && laborSection.lines.length > 1) {
var previousLaborLine = laborSection.lines[laborSection.lines.length - 2];
if (previousLaborLine['SVMXC__End_Date_and_Time__c'] &&
previousLaborLine['SVMXC__End_Date_and_Time__c'].fieldvalue &&
previousLaborLine['SVMXC__End_Date_and_Time__c'].fieldvalue.value &&
previousLaborLine['SVMXC__End_Date_and_Time__c'].fieldvalue.value.length > 0) {
var lastLineEndTime = previousLaborLine['SVMXC__End_Date_and_Time__c'].fieldvalue.value;
request.details = request.details || {};
request.details[laborSectionId] = request.details[laborSectionId] || [];
request.details[laborSectionId].push({
'index': '' + laborSection.lines.length - 1,
'fields': [{
'name': 'SVMXC__Start_Date_and_Time__c',
'value': lastLineEndTime
}]
});
$sfm_records.setFieldValue(request, result => {
console.log(JSON.parse(result));
$response({
status: 'success'
});
});
} else {
$response({
status: 'fail',
error_message: 'Last Labor line End date time is empty '
});
}
} else {
//No Labor lines. We can add record with out snippet changes
$response({
status: 'success',
error_message: ''
});
}
} else {
$response({
status: 'error',
error_message: 'No Labor section found'
});
}
} else {
$response({
status: 'error',
error_message: 'No detail lines found'
});
}
} else {
$response({
'status': 'error',
'error_message': 'Invalid response from $sfm_records.get API'
});
}
});
Was this helpful?