You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
*If your collection variable is longer than 20 items, and wish to get the compiled query string, make sure to make use of thenew Query.bind(':varName').
Map<Id,sObject>accts =newMap<Id,sObject>([SELECTIDFROMAccount]);// variable name declared hereSet<Id>acctIds =accts.keySet();Queryq =newQuery() .fromSObject('Account') .fields(newList<String>{'Id','Name' })// is passed in here .whereFilter('ID','IN',newQuery.bind(':acctIds')) .limitRows(200);for(Accountsobj :Database.query(q.queryString())) {// processing here }}
// optsQuery.Optionsopts =newQuery.Options(false,true,AccessType.READABLE );// cross object filtersList<Query.Filter>contactFilters =newList<Query.Filter>();contactFilters.add(newQuery.Filter('LeadSource','=','Purchased List'));contactFilters.add(newQuery.Filter('DoNotCall','=',false));List<sObject>res =newQuery(opts) .fromSObject('Account') .fields(newList<String>{'Id','Name'}) .subQuery('Opportunities',newList<String>{'Id','StageName'}) .whereFilter('ID','NOT IN',newQuery.CXFilter('Contact','AccountId',contactFilters,'OR' )) .run();// runs/*SELECT Id, Name, (SELECT Id, StageName FROM Opportunities)FROM AccountWHERE ID NOT IN (SELECT AccountId FROM Contact WHERE LeadSource = 'Purchased List' OR DoNotCall = false)*/
Javascript
Construct dynamic queries in Javascript,
Object key/prop names are case insensitive. Should lead to less debugging and confusion.
*.js
constpayload=JSON.stringify({options:{securityEnforced:true,stringInaccessible:false,AccessType:'CREATABLE'},fields:['Id','Name'],subQueries:[{sObjectName:'Contacts',fields:['Id','Name'],condition:{filters:[{fieldName:'LeadSource',operator:'=',value:'Web'},{fieldName:'Name',operator:'=',value:'Jack Rogers'},{fieldName:'Email',operator:'!=',value:null}],logic:'(1 OR 2) AND 3'}}],sObjectName:'Account',condition:{filters:[{fieldName:'ID',operator:'IN',cxValue:{sObjectName:'Opportunity',fieldName:'AccountId',condition:{filters:[{fieldName:'StageName',operator:'=',value:'Closed Won'}]}}},{fieldName:'ID',operator:'NOT IN',cxValue:{sObjectName:'Contact',fieldName:'AccountId',condition:{filters:[{fieldName:'LeadSource',operator:'=',value:'Purchased List'},{fieldName:'DoNotCall',operator:'=',value:false}],logic:'OR'}}}],logic:'AND'},limitRows:200});/** **/visualforce.remoting.invokeAction(...,payload,(result,event)=>{..});
[ApexController].cls
publicclassApexController {@RemoteActionpublicstaticList<sObject>queryJSON(stringjsonStr) {/* Pass in json string */Queryq =newQuery().fromJSON(jsonStr);/* Can pass in Map<String, Object> Map<String, Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jsonStr); Query q = new Query().fromJSON(jsonMap); */returnq.run(); }}