You need to sign in to do that
Don't have an account?
Batch Apex Query problem
Hi, I am
working on developing batch apex code for assigning territories in bulk
to leads, accounts, opportunities etc on clicking a custom button
called Run Rules.
My problem is that what will be the query for start method? As I
want all related accounts, opportunities, leads to have territories
assigned to them as per rules in code, how will I map same in Batch
apex? In query, I think we can only extract records of 1 object and
pass to execute batch method. For all accounts, opps, leads what will
be the query?
The skeleton non-working code is as follows:-
global class BatchApexTerritoryAssignmentBulk implements Database.Batchable<SObject>
{
String query;
global database.Querylocator start(Database.batchableContext bc)
{
SObject[] s1 = [select name, id from Account];
SObject[] s2 = [select name, id from Lead];
query = ??;
/*How to create query of records as on click of button all records
associated with territory need to be selected for batch apex. So we
can't create
query only for account or only for lead, how to take both into consideration?*/
return Database.getQueryLocator(query);
}
/*Depending on query parameter needs to be passed in execute method. So what should be 2nd parameter in execute method?*/
global void execute(Database.BatchableContext BC, List<SObject> sobjList)
{
/*If the required sobjList is obtained successfully, then filter out for leads, accounts, etc*/
List<Lead> leadList = new List<Lead>();
for(sobject s : sobjList)
{
if(s.getsObjectType == Lead.sObjectType)
{
Lead l = (Lead)s;
TerritoryAssignmentBulk.runRules(null, null, l, false);//The
method that needs to be called, for lead all others will be null and
boolean false
leadList.add(l);
}
}
update leadList;
List<Account> accList = new List<Account>();
for(sobject s : sobjList)
{
if(s.getsObjectType == Account.sObjectType)
{
Account a = (Account)s;
TerritoryAssignmentBulk.runRules(a, null, null, false);//for account, others will be null and boolean false
accList.add(a);
}
}
update accList;
}
global void finish(Database.BatchableContext BC)
{
}
webservice static String BatchApexMethod()
{
BatchApexTerritoryAssignmentBulk inst1 = new BatchApexTerritoryAssignmentBulk();
Id batchprocessid = Database.executeBatch(inst1);
return '';
}
}
Please assist!
Thanks!
-Vimal
When you create your batch apex class, give the variable 'query' a value of 'Select Id, Name From Account' or 'Select Id, Name From Lead' etc.
I'm thinking you will have to create an instance of the batch apex class for every object type. e.g. :
BatchApexTerritoryAssignmentBulk leadBulk = new BatchApexTerritoryAssignmentBulk(); leadBulk.query = 'Select Id, Name From Lead'; Id leadProcessId = Database.executeBatch(leadBulk); BatchApexTerritoryAssignmentBulk accountBulk = new BatchApexTerritoryAssignmentBulk(); accountBulk.query = 'Select Id, Name From Account'; Id accountProcessId = Database.executeBatch(accountBulk);
Can't see any other way to do it.