Search code examples
salesforcevisualforceapexforce.comremoteobject

Visualforce RemoteObjectModels Query filtering using "OR"


I am using in visualforce page of Salesforce.com. For demo purposes, I have used the following code snippet from the example docs shown in

http://docs.releasenotes.salesforce.com/en-us/spring14/release-notes/rn_vf_remote_objects.htm

In my code snippet i have a 'Where' clause in which i am trying to filter using 3 fields. My requirement is that the records must match the criteria A or criteria B or criteria C.

Code Example

<apex:page >
    
    <!-- Remote Objects definition to set accessible sObjects and fields -->
    <apex:remoteObjects >
        <apex:remoteObjectModel name="Group_Donor__c" jsShorthand="Groupdonor" 
            fields="Name,Id">
            <apex:remoteObjectField name="State__c" jsShorthand="State"/>
            <apex:remoteObjectField name="Org_Phone__c" jsShorthand="Phone"/>
            <apex:remoteObjectField name="Billing_Type__c" jsShorthand="billingtype"/>
        </apex:remoteObjectModel>
    </apex:remoteObjects>

    <!-- JavaScript to make Remote Objects calls -->
    <script>
        var fetchWarehouses = function(){
            // Create a new Remote Object
            var wh = new SObjectModel.Groupdonor();
            
            // Use the Remote Object to query for 10 warehouse records
            wh.retrieve({
                where: { 
                          or: {
                                Name : {like:"%Helloworld%"}, // Error
                                State: {like:"%chennai%"},
                                //Phone: {like:"%098765432344%"}, 
                                billingtype: {like:"%Credit Card%"}
                              } 
                          }, 
                limit: 10 , 
            }, function(err, records, event){
                if(err) {
                    alert(err.message);
                }
                else {
                    var ul = document.getElementById("warehousesList");
                    records.forEach(function(record) {
                        // Build the text for a warehouse line item
                        var whText = record.get("Name");
                        whText += " -- ";
                        whText += record.get("Phone");
                        whText += " -- ";
                        whText += record.get("billingtype"); 
                        
                        // Add the line item to the warehouses list
                        var li = document.createElement("li");
                        li.appendChild(document.createTextNode(whText));
                        ul.appendChild(li);
                    });
                }
            });
        };
    </script>

    <h1>Retrieve Group Donors via Remote Objects</h1>

    <p>Warehouses:</p>

    <ul id="warehousesList">
    </ul>
    <button onclick="fetchWarehouses()">Retrieve Group Donors</button>

</apex:page>

When i execute this code i get the following error.

Error Message :

Invalid criteria specified for retreival. ValidationError [code=11, message=Data does not match any schemas from &quot;oneOf&quot; path=/where, schemaKey=null]

This issue occurs only during the following conditions.

  1. When i use Standard field like Name in the OR condition. ( Even 2 or 1 filter)
  2. When i use more than 3 Custom fields in the OR condition ( More than 2 Query filter)

But when i use just any 2 custom fields mentioned in the RemoteObjectModel as filters, i get the expected results.

Kindly let me know what am i missing here. If i have use more than 2 filters in or condition, how do i achieve it ? is the usage of 'OR' proper in the remote-objects?. And has anyone come across this issue. if so kindly provide me some pointers.

Thanks in advance.


Solution

  • I've been doing some looking and there's some bad news and some good news.

    First, it's a (obscure)known limitation that you can't have more than 2 predicates for AND and OR queries - Docs here

    However, you seem to have discovered another bug in that an Standard Field (Name, Id) seems to not work when used with a custom one. My workaround was to redefine ALL fields, even standard ones like this:

    <apex:remoteObjectModel name="Group_Donor__c" jsShorthand="GroupDonor">
        <apex:remoteObjectField name="Name" jsShorthand="NameJS"/>
        <apex:remoteObjectField name="State__c" jsShorthand="State"/>
        <apex:remoteObjectField name="Org_Phone__c" jsShorthand="Phone"/>
    <!--.... etc-->
    

    At least you'll be able to query standard fields this way.

    As an ultimate work around, you'll probably have to retrieve two lists of records and use JavaScript to create your ultimate OR list.

    Good luck!