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
Phani PYDIMARRY 11Phani PYDIMARRY 11 

Compare Account Name inside a Batch class to process records

I would want to write a batch class to consider some Account records which have a flag unchecked and then compare them with a duplicate Account records created with the same name which are checked to update other values. I am new to APEX please let me know how can I frame the code.
Global class myBatch implements Database.Batchable<sobject>{
    Global myBatch(){
        }
global Database.Querylocator start(Database.Bachablecontext context){

   return Database.getQuerylocator([Select ID, Name from Account where BATCH_PROCESS__c = FALSE]);
}

global void execute(Database.BatchableContext BC, List<Account> scope){
    //I would want to get those Account names which have a BATCH_PROCESS__c AS TRUE to compare 

list<Account> acc = ([Select Name from Account where BATCH_PROCESS__c = TRUE]);
    set<string> st = new set<string>();
    for(Account act: acc){
    st.add(act.name);
}
 try{
     
     for(Account active:scope){
         
         //I am struck here on how to compare the values from the string
     }
  }catch(Exception e){}
}
}

 
TechingCrewMattTechingCrewMatt
global class myBatch implements Database.Batchable<sobject>{
    global List<Account> start(Database.Batchablecontext context){
   		return new List<Account>([SELECT Id, Name FROM Account WHERE BATCH_PROCESS__c = FALSE]);
	}

	global void execute(Database.BatchableContext BC, List<Account> scope){
    	//I would want to get those Account names which have a BATCH_PROCESS__c AS TRUE to compare 
		Set<String> unprocessedAccountNames = new Set<String>();
        for(Account unprocessedAccount : scope){
            unprocessedAccountNames.add(unprocessedAccount.Name);
        }
    	List<Account> processedAccountDuplicates = new List<Account>([
            SELECT 	Id 
            FROM 	Account 
            WHERE 	BATCH_PROCESS__c = FALSE AND 
            		Name IN :unprocessedAccountNames
        ]);
        for(Account processedAccountDuplicate : processedAccountDuplicates){
            //do stuff with the accounts
        }
		update processedAccountDuplicates;
	}
    
    global void finish(Database.BatchableContext context){}
}

Hello, Phani. You can have the start() method return the list of unprocessed Accounts. The execute() method can create a set of Account names and then query processed Accounts with the same name.
 
Phani PYDIMARRY 11Phani PYDIMARRY 11
Thanks for the reply, I see you have used List inside for loop. In my knowledge it may blow up the governor rules. Well I am thinking of using maps to contain the name and compare. what would you say?
TechingCrewMattTechingCrewMatt
I don't see a potential issue with the governor limits with the code provided. I wouldn't suggst querying very Account with the checkbox checked - it is inefficient and more likely to exceed the limits. You should only query Accounts that are potential duplicates.
sachinarorasfsachinarorasf
Hi Phani,

Use bellow code for compare the values from the string

Global class myBatch implements Database.Batchable<sobject>{
    Global myBatch(){
        }
    global Database.Querylocator start(Database.Bachablecontext context){

       return Database.getQuerylocator([Select ID, Name from Account where BATCH_PROCESS__c = FALSE]);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope){
        //I would want to get those Account names which have a BATCH_PROCESS__c AS TRUE to compare 

    list<Account> acc = ([Select Name from Account where BATCH_PROCESS__c = TRUE]);
        set<string> st = new set<string>();
        for(Account act: acc){
        st.add(act.name);
    }
     try{
         
         for(Account active:scope){
             // Check current Account name in your Set Which has checked Account Name
             if(st.contains(active.Name)){
                // Do Something
             }
         }
      }catch(Exception e){}
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
PM Modi SchemePM Modi Scheme
use this code available comment sections. simply paste this code and Hopley solve this problem.
Like me
https://naukrimint.com/
Phani PYDIMARRY 11Phani PYDIMARRY 11
I modified the code and I would explain. Please look into this. I am trying to compare both the lists and the update the scope. I am I doing it right?
I am trying to fin the Accounts with a combination of name and tracking number. Then I would want to update the records 
Global class myBatch implements Database.Batchable<sobject>{
    Global myBatch(){
        }
global Database.Querylocator start(Database.Bachablecontext context){

   return Database.getQuerylocator([Select ID, Name, tracking_number__c from Account where ID_AML__c = 'F' AND  BATCH_PROCESS__c = TRUE]);
}

gglobal void execute(Database.BatchableContext BC, List<Account> scope){
    // process the names you've got in your scope
    MAP<String,ID> accNames = new MAP<String,ID>();
    for(Account a : scope){
        accNames.put(a.Name,a.tracking_number__c);
    }
// I want the below list to be updated later when the condition matches
    list<Account> activeAcc = [Select ID, Name, tracking_number__c from Account where ID_AML__c = 'Y' AND  BATCH_PROCESS__c = FALSE
                                AND Name IN : accNames.KEYSET() AND tracking_number__c = accNames.values()];

    for(Account a: activeAcc){
        //I want to compare the list with the scope I am struck here
        if(accNames.keyset().contains(a.name)&& accNames.value().contains(a.tracking_number__c)){
        
        //
    }
    } 

}
}