Go > ServiceMax Go for Administrators > Configuring Core Features for Go > Mobile Timesheet > Review and Validate Timesheet using JS Code Snippet
Review and Validate Timesheet using JS Code Snippet
Technicians can review the warnings or errors in their timesheets and rectify them before submitting them. Timesheet data is evaluated using the custom javascript code when the timesheet is submitted. The following validation messages are displayed during the timesheet validation.
Warning: The timesheet can be submitted without resolving the warning conditions.
Error: The timesheet cannot be submitted without resolving the error conditions.
The admin must configure SET007 (Module: ServiceMax Go; Sub-Module: Timesheets) and set the value to a valid Code Snippet Id. By configuring this setting, the code snippet is downloaded during Config or Initial syncs.
* 
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.
For the Code Snippet Id, the admin should create a code snippet record in the SVMXC__Code_Snippet__c object. For more information, see Create and Configure Custom Code Snippet. The Code Snippet Id obtained should be assigned to the above setting SET007.
Sample Timesheet Validation Code Snippets
The following are some of the sample timesheet validation code snippets for a better understanding. The sample code snippet consists of a validation type and its corresponding error message.
Sample code 1: Specifies to stop validation if any error or warning is encountered.
$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" });
});
Warning Message on Timesheet
Sample code 2: Specifies validation results containing multiple errors or warnings.
$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);
}
},
);
});
Sample code 3: Specifies multiple validation errors or warnings in a single response.
$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);
}
});
Was this helpful?