Tuesday, January 7, 2025

How to process two independent object data in one Batch class Salesforce

 Hi All,

Today we will discuss how to process to object data which are independent (ie no relationship ).

for  that we will use List < sObject > scope = new List < sObject > ();

Sample Code:

public class BatchClassOn2Object implements Database.Batchable < sObject > {

  public List < sObject > start(Database.BatchableContext c) {

    List < sObject > scope = new List < sObject > ();

    scope.addAll([select id, F1__c, F2__c from Obj1__c]);

    scope.addAll([select Obj2_F2__c, Account_Name__c from Obj2__c]);

    return scope;

  }

  public void execute(Database.BatchableContext c, List < sObject > scope) {

    for (sObject obj: scope) {

      switch on obj {

        when Obj1__c obj1 {

          system.debug('====obj1' + obj1);

        }

        when Obj2__c obj2 {

          system.debug('====obj2' + obj2);

        }

      }

    }

  }

  public void finish(Database.BatchableContext c) {

}

}

 

No comments: