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
Soundhariyaa MSoundhariyaa M 

How to store a Object in list with the fields that is required for processing

Hi All,

I have a custom object A with custom fields a__c,b__c and d__c. I'm storing a List<A> and sending this list to a batch that is getting called in the finish method of the current batch.

Now, my requirement is to send this list<A> with fields that are required in the next batch processing [ in this case I need only a__c,b__c, and not d__c ].

Could anyone pls suggest  me the best way to achieve this?

One way I am thinking is to use SOQL Query but this is adding another query to my transaction.

Thanks in Advance!
Best Answer chosen by Soundhariyaa M
Maharajan CMaharajan C
Hi Soundhariya,

Iterate your List<A> ( AList in below)  in the finish method like below and only add the required fields to new List. And pass newA to your next batch.
global void finish(Database.BatchableContext bc){
    List<A__c> newA= new List<A__c>(); 
    for(A__c  a: AList){
        newA.add(new A__c(a__c= a.a__c, b__c = a.b__c ));
     }
    Database.executeBatch(new Batch2(newA));
}

Thanks,
Maharajan.C