We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [dynamic-soql]

Dynamic SOQL refers to the creation of a SOQL string at runtime with Apex code.

Filter by
Sorted by
Tagged with
71
votes
4answers
23k views

Elegant way to convert Set<Id> into String for Dynamic SOQL IN comparison

When I have a Set of Id's and I want to do a dynamic SOQL query that I want to use it in for an IN comparison, I have to convert the Set to string in the format of: ('id1','id2',id3',...) The way I ...
34
votes
5answers
34k views

Howto wildcard SELECT * to query all fields of objects in APEX?

I have created sObject and I need all the fields and I have many. Is there a way I can directly get all of them rather then typing each one of them.
9
votes
5answers
2k views

What's wrong with this SOQL

Declared a string and assigned SOQL value as shown below. While running I am getting an error. String country = 'United States'; String accountQuery = 'SELECT Id,BillingPostalCode,County_Name__c '+ ...
10
votes
2answers
8k views

What makes a field not groupable

I'm trying to use an aggregate query, but one of the fields I want to group by is not groupable (saw it in the soql error and then verified groupable=false in the metadata). It's just a number field ...
26
votes
2answers
5k views

Bind variable with dotted name not working in dynamic SOQL

As mentioned in this answer, it's possible to use bind variables in dynamic SOQL queries. This seems like a great way to avoid SOQL-injection, and to build a query string once but use it multiple ...
15
votes
4answers
2k views

How to find out if an Object is a collection or single value?

I have some dynamic SOQL code in which I'd like to identify whether a method argument is a collection or a single value: SObject[] query(SObjectType sobType, String field, Object data) { // Want ...
3
votes
1answer
2k views

Dynamic SOQL, Access Subquery Results

I'm having trouble understanding how to access each individual result returned from a subquery. The picture shows the code which: takes fields and objects chosen by a user and assigns them to String ...
28
votes
1answer
9k views

Getting “__r” relationship name from Schema.SObjectType.Fields

Say I have a master-detail relationship between two objects, Schematic__c and Part__c. I can SOQL query the master from the detail: [SELECT Schematic__r.Name FROM Part__c] If I'm building a query ...
6
votes
2answers
3k views

Dynamic SOQL Queries and Managed Namespaces

If I create a dynamic SOQL query (via strings, to be used with Database.query) in an unmanaged code environment, and then the code is converted to a managed environment, are namespace considerations ...
4
votes
1answer
670 views

Using single quote by curly braces in String.format in Dynamic SOQL

I'm struggling to use String.format to replace a token with a value when the token is within single quotes: Example: String query = String.format('SELECT Id FROM Availability WHERE Person__c = \'{0}\...
9
votes
1answer
18k views

How to use the escapeSingleQuotes method?

I'm trying to use this method in a dynamic SOQL query. Follow the sample code: public static String getRowById(String sobjName, id id) { String query = 'SELECT '; Map<String, ...
24
votes
1answer
580 views

SOQL bind variable scope behavior

After I posted this question, I found a similar post that seemed to answer it: Variable scope - database.query() has access to variables not in scope. I'll leave this in place for now, as I am not ...
6
votes
1answer
7k views

How to Search a text in all fields in all objects and return all values?

Can I find a string from all fields from all custom and standard objects and return the data (all field data) of all the records that matches for all the fields of those objects?? Also is there an ...
3
votes
2answers
1k views

create a map with keys = fields names and values = query result

I am trying to create a map from a result of a SOQL query, this map contains the name of the fields as keys and the values of the query as values for example: List accounts = [SELECT Name, Email ...
13
votes
3answers
25k views

Filtering by Long Text Area field in SOQL

I'm working on a custom search page, which will search through the fields of the Project object. I use dynamic SOQL query similar to that: String query = 'SELECT Name, Status__c ... FROM ...
8
votes
2answers
2k views

Apex CPU Time limit in Batch start() - is this a Query Timeout and how to cope with?

I have a Batch iterating over a large amount of records (with many fields) and 2 of their related child records. The start() method uses Dynamic SOQL to fetch them via QueryLocator to vary which ...
12
votes
1answer
16k views

SObject row was retrieved via SOQL without querying the requested field:

I am getting this error with the code below. I've looked into the other cases on here. In my case I am referring to a relationship name (__r) which I am assuming is pulling correctly. It's a field ...
7
votes
4answers
5k views

How do I SOSL across objects, matching all records with FIND clause?

To all the expert SOSL gurus lurking out there, I thought this will be an interesting one: I have some SObjects of different types and I want to return their Name values in a single query. In ...
5
votes
2answers
5k views

Concatenate fields in SOQL queries in the WHERE clause (no Apex)

I am trying to run SOQL statement in salesforce.schema tool of FORCE.COM the following query: Select c.LastName, c.FirstName From Contact c WHERE (FirstName+' '+LastName) In ('Good Name','Bad Name') ...
2
votes
2answers
3k views

How Get Parent Field in Attachment?

Please Help me... Account sObject is Parent of Attachment. Now I want to bring Read-only Field Custom field (isReadOnly__c). SELECT Id, Name, Account.isReadOnly__c FROM Attachment How It Gets ? ...
11
votes
4answers
11k views

Can a SOQL query selecting all columns be generated at runtime?

It seems like SOQL does not have an equivalent to SQL's "SELECT * FROM ..." (If SOQL has this syntax, show me and I will mark that as the answer.) To get around this limitation, I have been using a ...
8
votes
2answers
20k views

How to execute a dynamic SOQL query with a variable number of “in” terms?

I understand from answers like this Bind variable with dotted name not working in dynamic SOQL that binding Apex variables into dynamic SOQL is more limited than into static SOQL. I want to write ...
7
votes
5answers
1k views

Trying to LIMIT a SOQL Statement based upon projected Heap size

THE GOAL: Return as many rows as possible from a Remote method without hitting Heap limits. We have a Visualforce page calling into a @RemoteAction method in Apex to return sObjects to the page. The ...
7
votes
1answer
2k views

Force Dynamic Soql results to include fields with null value

So I've read a lot about this and it seems that queries run with Database.query(queryString) will skip over any field values set to null instead of returning the property set to null This has cause a ...
5
votes
2answers
2k views

Grouping in Visualforce Charts

I have created a visualforce charting barSeries and it renders the results that I want aside from products with values of zero for the month. The chart is a repeat business report that is meant to ...
3
votes
5answers
6k views

Dynamic SOQL variable binding not working for multiple records

Am building a dynamic SOQL where I am using a list of strings that represent record ids, here's an example similar to my class: String q = ''; q += 'SELECT Id FROM' + object; q += ' WHERE Id IN :...
2
votes
1answer
4k views

SOQL Dynamic Query on objects ( Dynamic sobject name in soql )

String str = 'Order'; List<SObject> listData = [Select Id From str ]; Can we query like this, I want to query dynamically
2
votes
0answers
396 views

How to dynamically query in Remote Object Model?

See my code comments in my controller for what I'm trying to do. This article might help as well: Overriding Default Remote Objects Operations Page <apex:page> <apex:remoteObjects &...
8
votes
4answers
893 views

Genuine SOQL Injection issue or False Positive?

Trying to work out if this a valid issue with my code, or just a false positive I can note in report for Security Review. I have provided a slightly simplified version below. The Id I am querying for ...
7
votes
3answers
3k views

Unable to perform a COUNT() query on object with many million records

I have a custom object with around 25.000.000 records and need to perform a single count query to find those records where a field has a certain value. I could get none of the following queries to ...
6
votes
2answers
26k views

use string array in IN clause in dynamic soql

I have an array of strings i'd like to use in the IN clause of a dynamic soql query. if i wasn't building the string at runtime i could simply use (from the SF docs): String[] ss = new String[]{'a', ...
4
votes
1answer
6k views

How to query the data that has been modified in last 30 MINUTES

we have Order__c as custom object. we are trying to extract the past 30 minutes record. where LastModifiedDate=TODAY and HOUR_IN_DAY(LastModifiedDate) > 0.5 is not working We tried SOQL : ...
4
votes
2answers
853 views

Order Field by List in SOQL SELECT

I have an aggregate query on the Lead object. The query returns a list of Leads grouped by website (custom field) in a specific order based on a MAX of a date attribute. Say once we iterate through ...
2
votes
3answers
416 views

Building dynamic query AND/OR

My question is continuation from here This is the sample SOQL query which I'm trying to build dynamically and I have dozens of fields, it has AND OR My question is: I'm trying to figure out how to ...
1
vote
1answer
6k views

SOQL for getting CaseComent for a case

I want to get the associated CaseComment's CommentBody for a list of cases. String queryFields = 'Id, Subject, CaseComment.CommentBody'; List<Case> cases = Database.query('select '+ ...
13
votes
5answers
4k views

SOQL query to return records with numeric values

I'm trying to write a dynamic SOQL query to get an object's record that has numeric value in a particular field. I'm trying to do something similar like below: Account acc; queryString = 'SELECT Id,...
10
votes
2answers
2k views

Accessing child records returned from SOQL via generic SObject

I'm currently querying the records of a custom object, including some related child records. Unfortunately the parent object is unknown, so the returned results from the query is a list of SObjects. ...
7
votes
1answer
2k views

QueryException: Non-selective query (it is selective)

I am trying to query a Custom Object called Lead_Assignment__c based on some criteria. Original query below: List<Lead_Assignment__c> leadAssignments = [ SELECT Segment__c, Queue_Name__c, ...
6
votes
3answers
1k views

Why I can not use Map in dynamic query

Map<Id, Account> maps = new Map<Id, Account>(); for(Account acc : [Select Id from Account]) { maps.put(acc.Id, acc); } String s = 'Select Id from Account Where Id IN : maps.keySet()'...
4
votes
2answers
7k views

Dynamically create SOQL query from sobject

I have an SObject say Account which has some not null fields. I want to select all the accounts matching those values. Is there a way I can create a dynamic SOQL query for this. Suppose the Account ...
4
votes
2answers
2k views

Bind a List variable in a Dynamic SOQL Query

Code 1 List<String> accNameList = new List<String>{'Acc1','Acc2','Acc3'}; List<Account> accountList = Database.query('SELECT Id FROM Account WHERE Name IN:accNameList'); Code 2 ...
4
votes
2answers
150 views

Can Apex SOQL query variable binding be done in a method other than the method which invokes Database.query?

Apex Commons has some brilliant solutions, but one of the things which I feel is missing is an object-oriented approach to the SOQL query's "WHERE" conditions. I'm presently working on my own ...
3
votes
1answer
2k views

How to SOQL query contacts by phone number when there are more than 100,000 contacts

I am receiving a System.QueryException when searching for contacts by phone number in an APEX class: An error occurred at element myRule_1_A2 (FlowActionCall). An Apex error occurred: System....
3
votes
1answer
348 views

SObjectType name conflict in queries

I'm creating lookup fields on an object dynamically using the Metadata API, saving the name of a field as a Custom Setting and building queries dynamically. This works perfectly, but there is a ...
2
votes
1answer
2k views

How to filter query for specific types of parent lookup?

for (SObject deletedObject : [SELECT Id, isDeleted, LastModifiedDate, parentId FROM Attachment WHERE isDeleted = true AND LastModifiedDate >= :timestamp ALL ROWS]) { It appeared, that recently ...
2
votes
2answers
293 views

Less verbose dynamic +=?

I'm trying to do something like this: sObject.Custom_Field__c += myNumericVar; But I'm using dynamic SOQL, so I can't hard-code the name of the fields. The following code works, but it's really ...
2
votes
2answers
719 views

Dynamic invocation (or emulation) of the hierarchy custom setting getInstance() method

I have a requirement to avoid any compile-time dependency on a managed package. Dynamic SOQL, SObjectType and SObject allow me to do this cleanly for normal objects. But now I need to access a public ...
2
votes
2answers
2k views

Dynamic Query - System.QueryException: expecting a colon, found '.'

Suppose I have one code, HI, which is mapped to these values: HIRT, HIFS,HISL,HIST,HIGC,HIAN,HIIS,HICV,HISR,HOLI. When I select HI those values are are passed into a dynamic soql query. For a small ...
1
vote
1answer
1k views

System.QueryException: error when button click second time?

vf page: <apex:page showHeader="false" sidebar="False" controller="textInputsCon" applyHtmlTag="true" > <apex:form > Input Text1 <apex:inputText value="{!inputText1}"/><...
1
vote
2answers
2k views

Can we query hidden fields in apex?

There is a field named "Email Opt Out" in the contact object. Can we query this field and put in the filter condition if the "Email Opt Out" field does not have both "Field-Level Security" and "Field ...