|
|
• The setting value must not contain single quotes.
• When using the standard create non-work order time entry UI, the data validation rules and code snippet for calculating the duration of time entry are part of the standard process.
• When using an SFM, the logic for data validation rules and JS code snippet for calculating the duration of time entry should be specified in the SFM.
|
$sfm_records.get(function(rec) {
// parsedRec.header is the SVMXC__Timesheet__c object
var parsedRec = JSON.parse(rec);
var totalHours = parsedRec.header["SVMXC__Total_Hours__c"];
if (!totalHours) {
// you can create an error object with validationType is "ERROR"
$response({
status: "error",
validationType: "ERROR",
error_message: "Total hours cannot be empty"
});
// immediately return if any error occurs
return;
}
if (totalHours > 40) {
// or ypu can create a warning object with validationType is "WARNING"
$response({
status: "error",
validationType: "WARNING",
error_message: "Total hours are more than 40 hours"
});
// immediately return if any warning occurs
return;
}
$response({ status: "success" });
});

$sfm_records.get(rec => {
// create a array to store all errors/warnings
const errors = [];
let totalHours = 0;
const parsedRec = JSON.parse(rec);
const { header: timesheet } = parsedRec;
const timesheetId = timesheet.Id;
const start = timesheet.SVMXC__Start_Date__c;
const end = timesheet.SVMXC__End_Date__c;
const startDate = start && start.fieldvalue.value;
const endDate = end && end.fieldvalue.value;
$db.get(
{
object: 'SVMXC__Timesheet_Entry__c',
fields: ['SVMXC__Duration__c', 'Id'],
filter: [
{
sequence: 1,
left_operand: 'SVMXC__Timesheet__c',
operator: '=',
right_operand: [timesheetId],
right_operand_type: 'Value',
},
],
},
(query, entryResponse) => {
if (!entryResponse) {
errors.push({
status: 'error',
error: 'ERROR',
// message detail should be inject from app js
error_message: 'Error occurs during the query of time entry.',
});
}
const { error, results: timeEntries } = entryResponse;
if (error) {
errors.push({
status: 'error',
error: 'ERROR',
// message detail should be inject from app js
error_message: 'Error occurs during the query of time entry.',
});
}
if (timeEntries && timeEntries.length > 0) {
timeEntries.forEach(entry => {
totalHours += entry.SVMXC__Duration__c ? +entry.SVMXC__Duration__c : 0;
});
}
if (!totalHours) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'ERROR',
error_message: 'Total hours cannot be empty',
});
}
if (!startDate) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'ERROR',
error_message: 'startDate cannot be empty',
});
}
if (!endDate) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'ERROR',
error_message: 'endDate cannot be empty',
});
}
if (startDate && endDate && new Date(startDate) > new Date(endDate)) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'ERROR',
error_message: 'startDate cannot be later than endDate',
});
}
if (totalHours > 40) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'WARNING',
error_message: 'Total hours are more than 40 hours',
});
}
if (startDate && endDate && new Date(startDate).getTime() === new Date(endDate).getTime()) {
// accumulate the errors or warnings
errors.push({
status: 'error',
validationType: 'WARNING',
error_message: 'startDate and endDate are on the same day',
});
}
if (!errors || errors.length === 0) {
// return success if no error/warning
$response({ status: 'success' });
} else {
// return the error objects
$response(errors);
}
},
);
});
$sfm_records.get(function(rec) {
var parsedRec = JSON.parse(rec);
var latitude = parsedRec.header["SVMXC__Latitude__c"];
var longitude = parsedRec.header["SVMXC__Longitude__c"];
var errors = [];
if (!longitude) {
errors.push({
status: "error",
error: "Validation_Error",
error_message: "You forgot to provide longitude"
});
}
if (!latitude) {
errors.push({
status: "error",
error: "Validation_Error",
error_message: "You forgot to provide latitude"
});
}
if (errors.length === 0) {
return $response({
status: "success"
});
} else {
return $response(errors);
}
});