6

I cannot use the String[] ss inside the SELECT statement. It is giving me errors:

unexpected token: ':'

Code

public void getSubscribers(Id listId){
    String[] ss = new String[]{'Id', 'Name'};
    Map<String, String> fields = DopplerListCriteriasUtil.getAvailableLookupObjects();
    for (String e: fields.keySet()) {
            ss.add(e);
    }
    Map<Id, DopplerListSubscriber__c> suscMap = new Map<Id, DopplerListSubscriber__c>([
        SELECT :ss DopplerList__c FROM DopplerListSubscriber__c WHERE DopplerList__c = :listId
    ]);
    String concatSql = null;
}

This statement is giving me errors on :ss

Map<Id, DopplerListSubscriber__c> suscMap = new Map<Id, DopplerListSubscriber__c>([
    SELECT :ss DopplerList__c FROM DopplerListSubscriber__c WHERE DopplerList__c = :listId
]);

What is wrong?

6

You'll need to use a dynamic query for that: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

Here's an example from the article to show a bit about how it works:

String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');

Basically, you'll just update it to look more like this:

String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id, ' + ss + ' FROM MyCustomObject__c WHERE Name = :myTestString');
| improve this answer | |
  • Hello mate, I saw that example, but its not so clean, specially when I use a map, I though there was another option. – BoDiE2003 Mar 21 '17 at 19:31
13

You have to use dynamic SOQL if you want to add fields dynamically. Merge syntax is not supported.

List<String> fields = new List<String> { 'Id', 'Name' };
String soql = 'SELECT ' + String.join(fields, ',') + ' FROM MyObject__c WHERE ...';
List<MyObject__c> records = Database.query(soql);

I actually often have dynamic filters as well when I'm in such a scenario, and might use a similar strategy there. Another basic example:

List<String> fields = new List<String> { 'Id', 'Name' };
List<String> filters = new List<String> { 'CreatedDate = TODAY', '...' };

String soql = String.format('SELECT {0} FROM MyObject__c WHERE {1}', new List<String> {
    String.join(fields, ', '), String.join(filters, ' AND ')
});
List<MyObject__c> records = Database.query(soql);
| improve this answer | |
  • 1
    nice implementation tips – Santanu Boral Mar 21 '17 at 20:16
  • I could have follow this approach – Santanu Boral Mar 21 '17 at 23:17
5

Salesforce does not support variable binding in the SELECT clause.

From Using Apex Variables in SOQL and SOSL Queries:

SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind. The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement. Bind expressions can be used as:

  • The search string in FIND clauses.
  • The filter literals in WHERE clauses.
  • The value of the IN or NOT IN operator in WHERE clauses, allowing filtering on a dynamic set of values. Note that this is of particular use with a list of IDs or Strings, though it works with lists of any type.
  • The division names in WITH DIVISION clauses.
  • The numeric value in LIMIT clauses.
  • The numeric value in OFFSET clauses.

Bind expressions can't be used with other clauses, such as INCLUDES.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.