function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ClaiborneClaiborne 

Question about Batch Process

I have a question about what can be done in a batch execution process.

 

Typically, the batch process is something like this:

 

 

for (sObject s : scope) {
   do stuff here
} update scope;

 

 

 

But can you do things based on the object in scope.

 

 

for (sObject s : scope) {
   do stuff here

      // Other stuff
   List<sObject> nsoList = [select based on value in s];
   for (SObject nso : nsoList) {
      do stuff here on nso
   }
   update nsoList;
}
update scope;

 

 

What if you set the batch size to 1?

 

Would this work?

 

Any ideas?

 

 

mikefmikef

You can do that type of logic, just like you can drive the wrong way down a one way street, and you will run into issues.

The first issue you will come across is the infamous SOQL query limit, we all love that one.

 

So in order to beat Apex at it's own game you would need to build maps and then you can do your processing based of the in memory map and not querying in every iteration of your outter for loop.

 

 

Set<Id> nsoIds = new Set<Id>();
Map<Id, nso> nsoMap;
nso tmpNso;
List<nso> nsoList = new List<nso>();

for(sObject s : scope){
  if(s.nso__c != null){
    nsoIds.add(s.nso__c);
  }
}

nsoMap = [select Id, [whatever] from nso where Id in : nsoIds];

for (sObject s : scope) {
   do stuff here

   tmpNso = nsoMap.get(s.nso__c);
   tmpNso. //whatever code you want.
   nsoList.add(tmpNso);
}
update nsoList;
update scope;

 

Something like that.

 

BritishBoyinDCBritishBoyinDC

As far as I can tell, each execution of a batch has it's own separate limits - so in that way, it operates much like a trigger would - if a batch process works for one 'batch' of 200 records, it will work for however many records you pass into it.

 

If you hit limit issues, you can always reduce the size of the batch, but as Mike says, you still can't bypass the normal limits when processing each batch...so you can do a query each execution of a batch, and execute an update, but the query can't return more than 10,000 rows during any one execution...

CLKCLK

i would suggest two way for doing it

 

1st - Dont query in "for loop". it can fall into governer limits

As u r saying specifieng scope to 1 but it will slow down the process.

Collect all "A object" value whatever required for another "B object" query.

and make a query on set of values.

 

2nd - For quering "B object" through execute call of  "A Object batch job" , write another batch job for this logic of "B object".

 

And call "B object" batch job within "A object".batch job execute method.

But remember don't call executebatch method through loop statement.

it can fall into governer limits as only five jobs are allowed.