+ Start a Discussion
S SaiS Sai 

Batch Class Urgent

how to move contacts one account to another account in salesforce using batch class
Prithviraj_ChavanPrithviraj_Chavan
Hi S Sai,
Is there any how will you detect that which account is related to which contact?
 
MayankAdmMayankAdm
Hi Sai,

Let me know your scenario how you will map one account to the another account in whch you have to move contact.

Thanks,
MayankAdmMayankAdm

Is there any flag in your account which tell this particular account is old or new?
Prithviraj_ChavanPrithviraj_Chavan
Hi Sai,
global class UpdateAccount implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID,old_acnum__c,accountnumber FROM ACCOUNT'
        );
    }

    global void execute(Database.BatchableContext bc, List<Account> lstAccount){
        // process each batch of records
        List<Account> lstAccounts = new List<Account>();
		lstAccounts1 = [SELECT ID,accountnumber,old_acnum__c,(SELECT ID FROM Contact) FROM ACCOUNT];
		Map<ID,ID> mapAccountContactId = new Map<ID,ID> mapAccountContactId();
        for ( Account account : lstAccounts ) {
            for ( Account account1 : lstAccounts1 ) {
                if( account.Id != account1.Id && account.old_acnum__c == account.accountnumber ){
					//Some Logic
				}
            }
        }
        update contacts;
    }    

    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        // call some utility to send email
        EmailUtils.sendMessage(a, recordsProcessed);
    }    

}

apply some of your logic .. in this way we can do this
brahmaji tammanabrahmaji tammana
Hi Sai,

Here is the code: 
global class ForumBatch9060G000000I9mcQAC implements Database.Batchable <sObject>{
    
    global Map<Id, List<Account>> accMap = new Map<Id, List<Account>>();
    global Map<Id, Id> accMapId = new Map<Id, Id>();
    global Map<Id, List<Contact>> accConMap = new Map<Id, List<Contact>>();
    global List<Contact> newConList = new List<Contact>();
    
    global Database.QueryLocator start (Database.BatchableContext BC){
        return Database.getQueryLocator([select Id,OldAccNum__c from Account where OldAccNum__c != null]);
    }
    
    global void execute (Database.BatchableContext BC, List<Account> accList){
        system.debug('entered execute');
        for(Account a: accList){
            system.debug('entered first for');
            accMapId.put(a.Id, Id.valueOf(a.OldAccNum__c) );
        }
        List<Account> oldAccCon = [select Id, (select Id, LastName, FirstName, Email from Contacts) from Account where id in: accMapId.values()];
        system.debug('finished first for');
        for(Account a : oldAccCon){
            accConMap.put(a.Id, a.Contacts);
            system.debug('entered second for');
        }
        
        for(Account a : accList){
            system.debug('entered thrid for');
            for(Contact c: accConMap.get(accMapId.get(a.Id))){
                system.debug('entered thrid innder for');
                Contact cnew = new Contact();
                cnew.AccountId = a.Id;
                cnew.LastName = c.LastName;
                cnew.FirstName = c.FirstName;
                cnew.Email = c.Email;
                newConList.add(cnew);
            }
        }
        if(!newConList.isEmpty()){
            insert newConList;
        }
    }
    global void finish(Database.BatchableContext BC){
        system.debug('finsh');
    }
    	

}

Mark it as best answer if it helps you.

Thanks
Brahma
Prithviraj_ChavanPrithviraj_Chavan
In start method update query like SELECT ID,accountnumber,old_acnum__c,(SELECT ID FROM Contact) FROMACCOUNT