[SalesForce] How to use getQueryLocator(sObject[])

I'm getting an error 'Argument must be an inline query' on the return line. Does that mean I need to make a query of my sObject in the parameter of the getQueryLocator() as opposed to passing in a variable for a List of opportunities?

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

where List<Opportunity> woList = new List<Opportunity>(trigger.new);

I would use my query there, but I'm writing a batchable class for a before Insert trigger. I can't think of anyway to write the query to identify the opportunities in trigger.new as they don't have Ids assigned yet.

Within context, I am trying to lookup a value passed in on an Opportunity (fs_wmCompanyId__c). I will then need to look to the account table to see if any account has that same value stored in a field (fs_wmCompanyId__c) on that table; if there is I will need the account ID. I will then loop back to the inserted Opportunity and set the account Id in the rs_client__c field.

I have it on a before insert trigger to ensure the rs_client__c field is set prior to saving the record; this was to make sure no validation rules prevent the update after save and that no integrations with other systems sync the data without the rs_client__c field. I figured the trigger should call a batchable class in case several hundred records were inserted at once. This is my first piece of code, so feel free to rip it apart and critique.

Best Answer

You actually need to provide a query string to the QueryLocator. If you want to use a collection instead, try using Iterable.

global Iterable start(Database.BatchableContext info){ 
    return woList; 
}  

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

Related Topic