Scripting - Querying Salesforce Records
Salesforce records and fields can be queried from script plugins.
The example below queries Opportunity Products and displays the query results in a textarea.
<div style="font-family:'Arial';font-size:16px;" id="roomDetails"></div><textarea id="queryResponse" cols="100" rows="30"></textarea>
<script> console.log( roomContext ); document.getElementById("roomDetails").innerText = 'Room Context: ' + roomContext.id + ' URL: ' + roomContext.url + " Session ID: " + roomContext.sessionId; document.getElementById("queryResponse").innerText = 'Loading data...';
var url = roomContext.url + '/query';
var queryRequest = {}; queryRequest.query = 'SELECT Id, Name, Description, Quantity, UnitPrice, TotalPrice, Opportunity.Name, ProductCode, Product2.Name FROM OpportunityLineItem WHERE OpportunityId=\'' + roomContext.masterRecordId + '\' LIMIT 100';
var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", url ); xmlhttp.setRequestHeader("Content-Type", "application/json"); xmlhttp.setRequestHeader("Authorization", roomContext.sessionId); xmlhttp.send(JSON.stringify( queryRequest ) ); xmlhttp.onreadystatechange = function() { if ( xmlhttp.readyState === 4) { var data = JSON.parse( xmlhttp.response ); console.log(data); document.getElementById("queryResponse").innerText = JSON.stringify( data, undefined, 4 ); } }</script>
Query Request
The Query API endpoint expects a queryRequest object in the POST body.
var queryRequest = {};queryRequest.orgId // (Optional) Provided in REST path.queryRequest.roomId // (Optional) Provided in REST path.queryRequest.origin // (Reserved)queryRequest.storedProcedure // Reference to a custom metadata type (MDT) record that predefines a query.queryRequest.query // Required. A SOQL query.queryRequest.replaceParams; // (Optional) A map of replacement values applied to query.
Query Limitations
The following limitations and restrictions apply to script queries:
- Queries must begin with
SELECT
. For insert and update actions, see Script Records. - Queries must end with
LIMIT N
where N is an integer between 1 and 200. This is a security and performance precaution to ensure the CX is optimally focused on small, focused data sets. - Queries may be dynamically generated in Javacript. But Apex variable convention is not supported, such as passing
WHERE Field = :variable
; - Queries may include relationship fields up to one level. Example
SELECT Product2.Name FROM...