Scenario 4: To Update the Start DateTime/ End DateTime of the Header Record using the child record DateTime values
A technician enters the Start Datetime or End Datetime of the child records. The Start Datetime/ End Datetime of the header record will be updated using some calculations in the custom code snippet.
Sample Snippet
This sample code snippet is to update the Start Datetime field of the header record using the Start Datetime of the Labor Usage line. The snippet is configured on the “On Button Click” event of the Work Order record. The least Start Datetime of the labor line is identified and updated in the Start Datetime field of the header record.
$sfm_records.get(sfmData => {
if (sfmData) {
sfmData = JSON.parse(sfmData);
var header = sfmData.header;
var details = sfmData.details;
if (details) {
var request = {};
var laborSection = null;
var laborSectionId = null;
var detailKeys = Object.keys(details);
for (var i = 0; i < detailKeys.length; i++) {
var eachDetail = details[detailKeys[i]];
if (eachDetail.name === 'Labor') {
laborSection = eachDetail;
laborSectionId = detailKeys[i];
break;
}
}
if (laborSection) {
if (laborSection.lines && laborSection.lines.length > 1) {
var lines = laborSection.lines;
var records = [];
for (var j = 0; j < lines.length; j++) {
var record = lines[j];
var laborStartDateTime = record['SVMXC__Start_Date_and_Time__c']
if (laborStartDateTime && laborStartDateTime.fieldvalue && laborStartDateTime.fieldvalue.value && laborStartDateTime.fieldvalue.value.length > 0) {
records.push(record);
}
}
if (records && records.length > 0) {
var sortedRecords = records.sort((a, b) => {
const first = a &&
a['SVMXC__Start_Date_and_Time__c'] &&
a['SVMXC__Start_Date_and_Time__c'].fieldvalue &&
a['SVMXC__Start_Date_and_Time__c'].fieldvalue.value;
const second = b &&
b['SVMXC__Start_Date_and_Time__c'] &&
b['SVMXC__Start_Date_and_Time__c'].fieldvalue &&
b['SVMXC__Start_Date_and_Time__c'].fieldvalue.value;
if (first === second) return 0;
return first < second ? -1 : 1;
});
var firstRecord = sortedRecords[0];
var labor_least_start_date_time = firstRecord['SVMXC__Start_Date_and_Time__c'].fieldvalue.value;
request.header = request.header || {};
request.header['fields'] = [{
'name': 'SVMXC__Scheduled_Date_Time__c',
'value': labor_least_start_date_time
}];
if (header && header.Id) {
$sfm_records.getPicklistValues(header.Id, 'SVMXC__Order_Status__c',
(response) => {
var result = JSON.parse(response);
if (result.status === 'success') {
var data = result.data || [];
if (data && data.length > 0) {
var firstRecord = data[0];
var statusValue = firstRecord && firstRecord.value;
request.header['fields'].push({
'name': 'SVMXC__Order_Status__c',
'value': statusValue
});
$sfm_records.setFieldValue(request, result => {
console.log(JSON.parse(result));
$response({
status: 'success'
});
});
}
} else {
$response({
status: 'error',
error_message: 'Invalid response from $sfm_records.getPicklistValues API'
});
}
});
} else {
$response({
status: 'error',
error_message: 'Invalid header record'
});
}
} else {
$response({
status: 'error',
error_message: 'Labor Lines Start Date Time is empty'
});
}
} else {
$response({
status: 'error',
error_message: 'No Labor Lines found'
});
}
} 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'
});
}
});