1

I'm still trying to figure out how batch work. My error is SYSTEM_METHOD_ENTRY [9]|Database.getQueryLocator(String).

This is what I have so far. I'm trying to upsert a list of records using apex batch. Your help is appreciated.

Combine__c is the external ID field.

My Batch Class:

global class batchUpsertRecords implements Database.Batchable<sObject> { 
    global final String query; 

    global batchUpsertRecords(Business__c[] busRec) {
       Business__c[] query = busRec;
    } 

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

    global void execute( Database.BatchableContext BC, Business__c[] scope){

        database.Upsert(Scope,business__c.Combine__c,false);
        Upsert scope; 

    }

    global void finish(Database.BatchableContext BC){
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob WHERE Id = :BC.getJobId()]     
    }

My Schedulable Class:

global class SFParAccelIntegration1 implements Schedulable{

    global void execute(SchedulableContext ctx)
    {
        SFParAccelIntegration1.combineIdBusiness();

    }


    public Static Void combineIdBusiness(){
        //Match CombineID to Account records and populate fields in Business object Salesforce section
        List<Business__c> businesses = new List<Business__c>();
        Set<string> combineIds = new Set<string>();

        for(Business__c bus : [Select Combine__c from Business__c limit 50000]) {
            combineIds.add(bus.Combine__c);
        }

        Business__c[] busRec = New Business__c[]{};

        //Get Account Data and Assign Values to Business
        For(Account a: [SELECT ID, Combine__c,Name,Total_Seat_Count__c,                 Number_of_FTE__c FROM Account WHERE Combine__c IN :combineIds]){
            busRec.add(New Business__c(Combine__c = a.Combine__c, 
             acctId__c = a.ID, SF_Total_Seat_Count__c = a.Total_Seat_Count__c,
             SF_Number_of_FTE__c = a.Number_of_FTE__c));

        }

        //system.debug(busRec);
        System.debug('combineIdBusiness1 Records Count - ' + busRec.size());

        //String query = 'SELECT Id,CreatedDate FROM Merchandise__c '; 
        batchUpsertRecords c = new batchUpsertRecords(busRec); 
        Database.executeBatch(c); 

    }

}
2

Your issue is that your're not storing the array passed in to the constructor into the member variable you're using in the start method:

// member variable is a String which is not what you want
global final String query; 

global batchUpsertRecords(Business__c[] busRec) {

    // this creates a new local variable called query which exists on the stack
    // and is removed when this constructor finishes
    Business__c[] query = busRec;
} 

global Database.QueryLocator start(Database.BatchableContext BC){

    // this is referencing the String member variable, which is still null
    return Database.getQueryLocator(query); 
} 

You'll need to run the query to select records inside of the Start method, so what you can do is change your code so that you pass in a set of Ids, and use that in the query:

// doesn't need to be visible outside of the batch class
private Set<Id> records;

global batchUpsertRecords(Set<Id> records) {
   this.records = records;
} 

global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator([select Id, Etc__c from Object__c
                                     where Id in : records]);
} 
  • Thanks @LaceySnr, I get a compile error at the line "return Database.getQueryLocator(query); " . The error is "Argument must be an inline query" – user7123 Feb 9 '15 at 23:35
  • Ah, I'd not actually looked at using that constructor this way before so didn't realise. Will update now. – Matt Lacey Feb 10 '15 at 23:24
  • 1
    Thanks Lacey, I resolved it by moving my query into my batch class and not passing it in. A little restructuring was required on my codes. I appreciate your help on this. – user7123 Feb 11 '15 at 23:55

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.