I have a scheduled batch that I am using to reassign accounts on a weekly basis as they come in. Using Accounts because they typically come in with more than 1 contact and the Leads object is hamstrung on this.

I have 3 classes: the schedule, the batch and the logic.

When I run the schedule nothing happens to the owner ID field. Also - I get none of the debug messages that I expect from the execute - but do from the Start and from the Schedule. Schedule:

global class AccountReassignSchedule implements Schedulable{

global void execute(SchedulableContext sc){

public static void runBatch(){
    string MLCOwnerID;
    list<MLCUser__c> U = MLCUser__c.getall().values();
    if(u.size()>0) {
        MLCOwnerID = u[0].UserID__c;
        System.debug('Sean \n in runBatch() Message follows:' + MLCOwnerID);
        String queryString = 'SELECT Id, OwnerId, BillingState, BillingPostalCode, DoNotAutoReassign__c FROM Account WHERE DoNotAutoReassign__c = false and OwnerId = ' + MLCOwnerID;
        System.debug('Sean \n in runBatch() Message 2 follows:' + queryString);

        //Call the batch
        AccountReassignBatch arBatch = new AccountReassignBatch(queryString);
        Database.executeBatch(arBatch, 100);


global class AccountReassignBatch implements Database.Batchable<sObject> {

global final String query;

global AccountReassignBatch(String q){
    System.debug('Sean \n message in AccoutReassignBatch follows:' + q);
    // here is where the class starts, reassign the local parameter to the global variable
    query = q;

global Database.QueryLocator start(Database.BatchableContext BC){
    // get the data in a single space
    return Database.getQueryLocator(query);
global void execute(Database.BatchableContext BC, List<Account> scope){
    //for each object in scope, check the address: the state and the zip, compare that to the available list of zips
    List<Account> acctsToUpdate = new List<Account>();
    System.debug('Sean \n message in execute follows:' + acctsToUpdate);
    CheckAddressToReassign CAtR = new CheckAddressToReassign();
    acctsToUpdate = CAtR.checkAddress(scope);
    System.debug('Sean \n message 2 in execute follows:' + scope);
    System.debug('Sean \n message 3 in execute follows:' + acctsToUpdate);
    update acctsToUpdate;

global void finish(Database.BatchableContext BC){}


In addition: I am getting a strange log message:

09:40:00.0 (7429346)|VARIABLE_SCOPE_BEGIN|[19]|this|AccountReassignBatch|true|false 09:40:00.0 (7453485)|VARIABLE_ASSIGNMENT|[19]|this|{"query":"SELECT Id, OwnerId, (131 more) ..."}|0x46aa752b 09:40:00.0 (7459594)|VARIABLE_SCOPE_BEGIN|[19]|BC|Database.BatchableContext|true|false 09:40:00.0 (7508295)|VARIABLE_ASSIGNMENT|[19]|BC|{"jobId":"7070v000002EIeiAAG"}|0x15f6a99c 09:40:00.0 (7519078)|STATEMENT_EXECUTE|[19] 09:40:00.0 (7520223)|STATEMENT_EXECUTE|[21] 09:40:00.0 (7538996)|HEAP_ALLOCATE|[50]|Bytes:5 09:40:00.0 (7558378)|HEAP_ALLOCATE|[56]|Bytes:5 09:40:00.0 (7565480)|HEAP_ALLOCATE|[64]|Bytes:7 09:40:00.0 (7930818)|EXCEPTION_THROWN|[21]|System.QueryException: unexpected token: MvX2 09:40:00.0 (7990464)|HEAP_ALLOCATE|[21]|Bytes:26 09:40:00.0 (8109690)|FATAL_ERROR|System.QueryException: unexpected token: MvX2

but this is in a standalone logmessage while a different one seems to indicate that everything is firing. That mvx2 is part of a users ID(15)

  • 1
    I think your Id need to be surrounded by single quotes. So you need to escape these single quotes using: and OwnerId = \'' + MLCOwnerID + '\''; Jul 31, 2017 at 15:25
  • Oh Martin, Thank you... I make "Rookie Mistake" look so endearing :(
    – SeanGorman
    Jul 31, 2017 at 15:32

2 Answers 2


You forgot to put quotes around the Id:

... OwnerId = '\'' + MclOwnerId + '\'';

I generally recommend using inline binding:

... OwnerId = :MCLOwnerId

As well as using inline queries instead of dynamic queries, when possible.


Exception occurs in your scheduled class.

Following similar snippet generates same error:

Id ownerId = 'XXXXXX';
String queryString = 'SELECT Id FROM Account WHERE OwnerId = ' + ownerId;

Try enclosing it in quotes.

Best would be only pass the variable and form your query in execute instead passing whole query.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .