$response API
$response API is used to respond with the status ('success'/'error') of the custom logic to the client application.
Following is the response when the request is successful.
$response({
status:'success’,error : '',error_message : ''});
Following is the response when the request fails.
$response({
status:’error’,error : ‘error’,error_message : ‘Invalid Parameters’}
);
Where
status is the status of the query execution. Possible values are 'success' and 'error'.
error is the type of error. Possible value is Validation_Error.
error_message is the error message to be displayed in the client application.
Example
The following example explains the recommended way to use the API.
$sfm_records.get(sfmData=>{

if (sfmData) {

$response({'status': 'success','error_message': ''});

} else {

$response({'status': 'error', error: 'error', error_message': 'SFM data is empty'});

}

});
From the above example, $response indicates that the snippet operation is complete with the callback.
The following example explains how the API should not be used.
$sfm_records.get(sfmData=>{
$response({'status': 'success','error_message': ''});
if (sfmData) {
//Continuing the logic.
$response({'status': 'success','error_message': ''});
} else {
$response({'status': 'error', error: 'error', error_message': 'SFM data is empty'});
}
});
Avoid using the $response() at the beginning of the snippet without completing the logic. Use this API only at the end of operation or logic with the status error or status.
relatedFields and relatedChildFields
The validation error message displayed in the SFM can be handled within JavaScript Snippet code by using the following keys in the $response API.
relatedFields is used to target the validation error to fields on the Header section.
relatedChildFields is used to target the validation error to the fields on the child section.
To target the correct fields on the child section, the section layout id and the Childline record id must be specified. Both the keys are optional.
* 
In web delivery, if the code does not specify where the error should appear, by default the validation error is displayed at the top in the header tooltip.
In mobile, if the code does not specify where the error should appear, by default the validation error is displayed in the Overview tab of the ServiceMax Go app.
The following template illustrates the target validation error to fields in the Header and Child Sections:
$response({
// result object
"error_message": "Some validation error occurred",
"validationType": "ERROR", // or WARNING
"relatedFields": [ // API names of related fields
"Header_Field__c"
],
"relatedChildFields": {
"1childsectionId1”: { // child line section id (labor/parts/etc type ids)
“2childrecordId2”: [ // record id
"Child_Field__c"
]
}
}
})
To view the sample code snippet on how the relatedChildFields property key can be used, see Display error message in the respective fields on the child section.
Was this helpful?