+ Start a Discussion
BoonPlusBoonPlus 

How to set the criteria parameter in the retrieve method for RemoteObjectController?

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_system_remoteobjectcontroller.htm

RemoteObjectController
Use RemoteObjectController to access the standard Visualforce Remote Objects operations in your Remote Objects override methods.

RemoteObjectController Methods
The following are methods for RemoteObjectController. All methods are static.
create(type, fields) Create a record in the database.
del(type, recordIds) Delete records from the database.
retrieve(type, fields, criteria) Retrieve records from the database.
updat(type, recordIds, fields) Update records in the database.

retrieve(type, fields, criteria)
Retrieve records from the database.
Signature
public static Map<String,Object> retrieve(String type, List<String> fields, Map<String,Object> criteria)
Parameters
type
Type: String
The sObject type on which retrieve is being called.
fields
Type: List<String>
The fields to retrieve for each record.
criteria
Type: Map<String,Object>
The criteria to use when performing the query.


My question is: How do set the criteria like 'where' and 'orderby' (in the java script below) in the retrieve method for RemoteObjectController
<apex:remoteObjectsjsNamespace="RemoteObjectModel">    
    <apex:remoteObjectModel name="Contact" fields="FirstName,LastName"/>  
</apex:remoteObjects>

<script>
var ct = new RemoteObjectModel.Contact();
ct.retrieve( 
    { where: { 
        FirstName: {eq: 'Marc'}, 
        LastName: {eq: 'Benioff'} 
      }, 
      orderby: [ {LastName: 'ASC'}, {FirstName: 'ASC'} ],
      limit: 1 },  

    function(err, records) { 
        if (err) { 
            alert(err); 
        } else { 
            console.log(records.length); 
            console.log(records[0]); 
        } 
    } 
);
</script>
 
public class TestRemoteActionOverrideController{
    @RemoteAction
    public static Map<String,Object> retrieve(String type, List<String> fields, Map<String, Object> criteria) {
        fields.add('Name');

        //
        // place holder for converting criteria below in java script to apex
        //
        // { where: { 
        // FirstName: {eq: 'Marc'}, 
        // LastName: {eq: 'Benioff'} 
        // }, 
        // orderby: [ {LastName: 'ASC'}, {FirstName: 'ASC'} ],
        // limit: 1 }
        
        // call through to the default remote object controller with our modified field list.
        return RemoteObjectController.retrieve(type, fields, criteria);
    }
}

 
SRKSRK

<apex:remoteObjects >
<apex:remoteObjectModel name=”Warehouse__c” jsShorthand=”Warehouse” fields=”Name,Id”>
<apex:remoteObjectField name=”Phone__c” jsShorthand=”Phone”/>
</apex:remoteObjectModel>
</apex:remoteObjects>
<apex:remoteObjects>

 

Retrieving records:
var wh = new SObjectModel.Warehouse();
wh.retrieve({ limit: 10 }, function(err, records){
if(err) alert(err.message);
else {
// Code to display records
}
Retrieve records with where condition:
var src = new SObjectModel.getActs();
src.retrieve({ limit : 10,
where : { cType :{eq : ‘Banking’}}
} ,
function(err,records){
if(err == null)
{
//Process returned “records” to display in Visualforce code.
}
} );

 

 

 

 

Important Notes:
1. Remote Objects respects your organization’s field level security settings. The fields that aren’t accessible to the person viewing the page appear blank. Actions that modify field data (create(), update(), and upsert()) fail with an error if they include inaccessible fields in the request.
2. Remote Objects use an object to specify criteria for retrieve() operations. Use this object to specify where, limit, and offset conditions for your queries.
The structured format of the query object enables Visualforce to validate the criteria at save time which reduces the likelihood of runtime errors.
var ct = new RemoteObjectModel.Contact();
ct.retrieve(
{ where: { FirstName: {eq: ‘Marc’},LastName: {eq: ‘Benioff’} },
orderby: [ {LastName: ‘ASC’}, {FirstName: ‘ASC’} ] limit: 1 },
function(err, records) {
if (err) {
alert(err);
} else {
}
}
);
The query criteria finds a contact named Marc Benioff and limits the query to a single result.
where Conditions:
where conditions enable you to filter the results of a retrieve operation, much the same way that a WHERE condition in a SOQL query does. The operators that are available for where conditions are:
eq: equals
ne: not equals
lt: less than
lte: less than or equals
gt: greater than
gte: greater than or equals
like: string matching. As with SOQL, use “%” as a wildcard character.
in: in, used for finding a value that matches any of a set of fixed values. Provide values as an array, for example, [‘Benioff’, ‘Jobs’, ‘Gates’].
nin: not in, used for finding a value that matches none of a set of fixed values. Provide values as an array, for example, [‘Benioff’, ‘Jobs’, ‘Gates’].
and: logical AND, used for combining conditions
or: logical OR, used for combining conditions
Within the where object, add field name and condition pairs to create complex criteria. Multiple conditions by default are treated as AND conditions. For example:
{
where:
{
or:
{
FirstName: { like: “M%” },
Phone: { like: ‘(415)%’ }
}
}
}
orderby Conditions
orderby enables you to set a sort order for your results and facilitates sorting up to three fields. Specify your orderby conditions as an array of JavaScript objects that contain name-value pairs. The field to sort on is the name, and the sort description is the value. The sort description enables you to sort ascending or descending and to sort null values first or last. For example:
orderby: [ {Phone: “DESC NULLS LAST”} , {FirstName: “ASC”} ]
limit and offset Conditions
limit and offset enable you to retrieve a specific number of records at a time and to page through an extended set of results.Use limit to specify how many records to return in one batch of results. The default value is 20. The maximum is 100.Use offset to specify how many records to skip in the overall result set before adding records to the returned results. The minimum is 1. There is no maximum limit.

BoonPlusBoonPlus
@SRK The question is about how to set the retrieve "Criteria" in Apex, not in VF page using Javascript.
BoonPlusBoonPlus
I was able to genereate the Map<String,Object> criteria with JSON.deserializeUntyped by following the instructions in the link below...
https://opfocus.com/json-deserialization-techniques-in-salesforce/

Unfortunately, my TestRemoteActionOverrideController did not work, it received a Salesforce system error.
Error: Salesforce System Error: 1732853427-40796 (-939844238) (-939844238)

If someone has a working example of the retrieve method override for RemoteObjectController, please post it here.