<apex:page> <!-- Remote Objects definition to set accessible sObjects and fields --> <apex:remoteObjects > <apex:remoteObjectModel name="Warehouse__c" jsShorthand="Warehouse" fields="Name,Id"> <apex:remoteObjectField name="Phone__c" jsShorthand="Phone"/> </apex:remoteObjectModel> </apex:remoteObjects> <!-- JavaScript to make Remote Objects calls --> <script> var fetchWarehouses = function(){ // Create a new Remote Object var wh = new SObjectModel.Warehouse(); // Use the Remote Object to query for 10 warehouse records wh.retrieve({ 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"); // Add the line item to the warehouses list var li = document.createElement("li"); li.appendChild(document.createTextNode(whText)); ul.appendChild(li); }); } }); }; </script> <h1>Retrieve Warehouses via Remote Objects</h1> <p>Warehouses:</p> <ul id="warehousesList"> </ul> <button onclick="fetchWarehouses()">Retrieve Warehouses</button> </apex:page>
<apex:remoteObjects > <apex:remoteObjectModel name="Warehouse__c" jsShorthand="Warehouse" fields="Name,Id"> <apex:remoteObjectField name="Phone__c" jsShorthand="Phone"/> </apex:remoteObjectModel> </apex:remoteObjects>
<!-- JavaScript to make Remote Objects calls --> <script> var fetchWarehouses = function(){ // Create a new Remote Object var wh = new SObjectModel.Warehouse(); // Use the Remote Object to query for 10 warehouse records wh.retrieve({ 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"); // Add the line item to the warehouses list var li = document.createElement("li"); li.appendChild(document.createTextNode(whText)); ul.appendChild(li); }); } }); }; </script>
The second line uses the new Warehouse object, wh, to perform a query for Warehouse records. The call provides two arguments: a simple query specifier and an anonymous function to handle the results. The function is standard JavaScript. It iterates over the results and creates list items to append to the list of warehouses on the page.
<h1>Retrieve Warehouses via Remote Objects</h1> <p>Warehouses:</p> <ul id="warehousesList"> </ul> <button onclick="fetchWarehouses()">Retrieve Warehouses</button>