$db API
$db API is used to perform operations on client database and retrieve records from local database.
$db.get(query_object, callback_function(query, response) { } );
query_object structure
{
object : ‘object_name’, fields : [‘field1’,'field2'…], filter : [filterCondition1,filterCondition2,…], Order : ['orderbyfield1’,'orderbyfield2',…], AdvancedExpression : “(1 and 2)” }
Where
object is a Salesforce API name of the object, on which the query will be executed
fields is a list of fields to be queried in the above object. Use Salesforce API name of the fields
filter is the list of conditions to be applied in the result set
filterCondition structure
{
sequence : sequence number,
left_operand : 'field_name',
operator:'operator',
right_operand : [
'field_name'/'value'
]
right_operand_type : 'Value'/'Field'
}
Where
sequence is the sequence number of the filter condition
left_operand is the field to be validated. Use Salesforce API name for the field.
operator - Possible values are =, <, > , <=, >=, in, between
right_operand is the field name or the value to compared with the left_operand.
• There can be only one right operand field/value for =, <, > , <=, >=operator.
• There can be any number of comma separated value/field for in operator.
• There can only be two comma separated value/field for between operator.
right_operand_type is to indicate the type of the right_operand. Possible values are 'Field' and 'Value'.
Order is the a list of field name, on which the sorting is applied
orderbyfield structure
orderbyfield1 : [{
queryField: 'field_name', sortingOrder: 'sorting order'}
],
Where
queryField is a field to be sorted
sortingOrder is the order in which the sorting can be performed. Possible values are ASC and DESC.
AdvancedExpression is the logical grouping of the filterCondition (Optional)
Response structure of the $db.get
{
status : 'status', results :[Record1,Record2, ..], error : '',
error_message : '' }
Where
status is the status of the query execution. Possible values are Success and Error
results is the set of records that qualifies the filter condition
error is the query execution error
error_message is the error message from the client database
Example
Following is the sample request for $db API:
var filterCondition1 =
{
"sequence": 1, "left_operand": "SVMXC__End_Date_and_Time__c", "operator": ">", "right_operand": [ "" ], "right_operand_type": "Value"
}
var filterCondition2 =
{
"sequence": 2, "left_operand": "SVMXC__Start_Date_and_Time__c", "operator": "<", "right_operand": [ "2019-02-07 06:31:00" ], "right_operand_type": "Value"
}
var filterCondition3 = {
"sequence": 3, "left_operand": "Id", "operator": "!=", "right_operand": [ "a2A_local_1549520636897_2" ], "right_operand_type": "Value"
}
var filterCondition4 =
{
"sequence": 4, "left_operand": "SVMXC__Line_Type__c", "operator": "=", "right_operand": [ "Labor" ], "right_operand_type": "Value"
}
var filterCondition5=
{
"sequence": 5, "left_operand": "RecordTypeId", "operator": "=", "right_operand": [ "Usage/Consumption" ], "right_operand_type": "Value"
}
var query_object = {
object : `SVMXC__Service_Order_Line__c`, fields : [`SVMXC__Start_Date_and_Time__c`, 'Name', ‘SVMXC__Service_Order__c’], filter : [filterCondition1, filterCondition2, filterCondition3, filterCondition4, filterCondition5], Order : [{
queryField: `SVMXC__Actual_Quantity2__c`, sortingOrder: 'ASC'}
], AdvancedExpression : '(1 AND 2 AND 3 AND 4 AND 5)' };
Following is the sample response for $dbAPI:
{
{
"status": "success", "results": [ {
"SVMXC__Actual_Quantity2__c": null, "SVMXC__Billable_Quantity__c": 0, "SVMXC__Start_Date_and_Time__c": "2019-02-05 10:00:00", "Name": "WL-00004091", “SVMXC__Service_Order__r”:"WO-00000198" }
], "error": "", "error_message": ""
}
}
Limitations of $db API
• It supports only SELECT database operation and does not support other database operations like INSERT, UPDATE, DELETE.
• This API will fetch only the ID of the lookup field or reference field value (first level object fields) and not the details of the lookup field (second level object fields).
Example: If you have to fetch work order related fields like work order number, ID and account, use the following sample response if you need second level object fields.
$db.get(query_object, function(query_object, response) {
$db.get(query_object1, function(query_object1, response1) {
});
});
where
response1 contains the second level selected field records.