0

I run the following code in the scheduled class:

        List<String> sObjFields = DynamicSOQL.getFields('Contact');
        String query2 = 'SELECT ' + sObjFields + ' FROM Contact';
        BatchApexContacts batch2 = new BatchApexContacts(query2);

Basically this gets a list of the fields on the Contact Schema and then we place it into the string list that we will then use to put inside of the query which will be used in the Database.queryLocator(). For some reason I am seeing the following error:

System.QueryException: unexpected token: accountid

I am 100% certain that this field exists on contact records. I just cannot wrap my head around why this is happening.

Here is the rest of the Batch apex.

public class BatchApexContacts implements Database.Batchable<sObject> , Database.Stateful 
{

    public String query;


    public BatchApexContacts(String soqlQuery) 
    {
        query = soqlQuery;

    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }



       public void execute(Database.BatchableContext BC, List<SObject> scope) 
        {
        }

    public void finish(Database.BatchableContext BC) 
    {

    }


}
1

You cannot append the list string. You need to parse the list into and string and then append it to the query string.

Here are the updates to the code:

List<String> sObjFields = DynamicSOQL.getFields('Contact');

String allstring = string.join(sObjFields,',');

String query2 = 'SELECT ' + allstring + ' FROM Contact';
| 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.