+ Start a Discussion
saurav nirala 1saurav nirala 1 

How to write batch apex using list parameter passed from flow

first class: 
global class FGContactOwnerUpdate1 implements Database.Batchable<sObject> {
List<Contact> Con = new List<Contact>();
List<Contact> ConList = new List<Contact>(); //Contact List to update
 
global FGContactOwnerUpdate1(List<Account> AccList){
        List<id> AccListId = new List<id>();
        for(Account ac: AccList){ 
        AccListId.add(ac.id);
    }
    } 
    
global static Database.QueryLocator start(Database.BatchableContext BC){
 string Con = 'select id, OwnerId, Account.ownerId from Contact where AccountId in :AccListId and Active__c = true';
    return Database.getQueryLocator(Con);
}  
    
   global void execute(Database.BatchableContext BC, List<Contact> conlist){
       for(Contact c: conlist){
    if(c.OwnerId != c.Account.OwnerId){
    c.OwnerId = c.Account.OwnerId;
     ConList.add(c); 
   }

Database.update(ConList);
}
}
    global void finish(Database.BatchableContext BC) {
  }
}

second class :
global class FGContactOwnerUpdate {
    
@InvocableMethod(label='exuete the batch ContactBatch' description='This method will be execute ContactBatch')
global static void executeBatchMethod(List<String> lst) {
    FGContactOwnerUpdate1 c1= new FGContactOwnerUpdate1();
    Database.executeBatch(c1, 50);
  }
    
}

Getting error as : Constructor not defined: [FGContactOwnerUpdate1].<Constructor>().

Its an urgent issue
Rishabh Bansal 23Rishabh Bansal 23
Hi saurav,
In the second class, you need to pass the accounts list to first class i.e batch.
FGContactOwnerUpdate1 c1= new FGContactOwnerUpdate1(AccList);
OR to save the class, declare a default constructor in the first class
global FGContactOwnerUpdate1(){
      
    } 

Thanks,
Rishabh