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
sales@myvarmasales@myvarma 

batch apex1

global class updatesaccount implements 
    Database.Batchable<sObject>, Database.Stateful
{
    global Integer recordsProcessed = 0;

   global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID, Phone, ' +
            'Fax, (SELECT ID, Phone, ' +
            'Fax FROM Contacts) FROM Account ' + 
            'Where Industry = \'Construction\''
        );
    }

    global void execute(Database.BatchableContext bc, List<Account> scope ){
        
      List<Contact> contacts = new List<Contact>();
        for (Account account : scope) {
            for (Contact contact : account.contacts) {
                contact.Phone = account.Phone;
                contact.Fax = account.Fax;
             
               
                
                contacts.add(contact);
               
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update contacts;
    }    

    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. pradeepvarma');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[] {'pradeep.gunturi@gmail.com'};
               mail.setSubject('Match Merge Batch ');
            mail.setPlainTextBody('hi pradeep batchapex completed');
         mail.setToAddresses(toAddresses);
       List<Messaging.SendEmailResult> sendRes = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, true);
        
       
    }    

}


i want copy phone and fax of account whos industry is construction

but when ever we create new contact its sshowing whats problem in prgrm
Best Answer chosen by sales@myvarma
Arvind KumarArvind Kumar
Hi Sales,

You can use this class. it will work for you.
 
global class updatesaccount implements 
    Database.Batchable<sObject>, Database.Stateful
{
    global Database.QueryLocator start(Database.batchableContext bc)
    {
        String accQuery = 'Select Id, phone, fax,(select id, phone, fax from Contacts) from Account where Industry =  \'Construction\'';
        return Database.getQueryLocator(accQuery);
    }
           
    global void execute(Database.batchableContext bc, List<Account> scope)
    {
                  
                  List<Contact> conList = new List<Contact>();
                  
                  for(Account accObj : scope)
                  {
                     for(Contact conObj : accObj.contacts)
                     {
                      conObj .Phone = accObj.Phone;
                      conObj .Fax = accObj.Fax;
                      conList.add(conObj);
                     }
                  }
                  
                  if(conList.size()>0 && conList != null)
                     update conList;
    }

    global void finish(Database.BatchableContext bc)
	{
        
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[] {'pradeep.gunturi@gmail.com'};
               mail.setSubject('Match Merge Batch ');
            mail.setPlainTextBody('hi pradeep batchapex completed');
         mail.setToAddresses(toAddresses);
       List<Messaging.SendEmailResult> sendRes = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, true);
        
       
    }    

}
If you have any query please let me know.

If it solves your problem then mark as a best answer.

Thanks,
Arvind Kumar
 

All Answers

Arvind KumarArvind Kumar
Hi Sales,

Please use this class. it will work for you.
 
global class updatesaccount implements 
    Database.Batchable<sObject>, Database.Stateful
{
    global Database.QueryLocator start(Database.batchableContext bc)
    {
        String accQuery = 'Select Id, phone, fax,(select id, phone, fax from Contacts) from Account where Industry =  \'Construction\'';
        return Database.getQueryLocator(accQuery);
    }
           
    global void execute(Database.batchableContext bc, List<Account> scope)
    {
                  
                  List<Contact> conList = new List<Contact>();
                  
                  for(Account accObj : scope)
                  {
                     for(Contact conObj : accObj.contacts)
                     {
                      conObj .Phone = accObj.Phone;
                      conObj .Fax = accObj.Fax;
                      conList.add(conObj);
                     }
                  }
                  
                  if(conList.size()>0 && conList != null)
                     update conList;
    }

    global void finish(Database.BatchableContext bc)
	{
        System.debug(recordsProcessed + ' records processed. pradeepvarma');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[] {'pradeep.gunturi@gmail.com'};
               mail.setSubject('Match Merge Batch ');
            mail.setPlainTextBody('hi pradeep batchapex completed');
         mail.setToAddresses(toAddresses);
       List<Messaging.SendEmailResult> sendRes = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, true);
        
       
    }    

}

If you have any query please let me know.

If it solves your problem then mark as a best answer.

Thanks,
Arvind Kumar
Arvind KumarArvind Kumar
Hi Sales,

You can use this class. it will work for you.
 
global class updatesaccount implements 
    Database.Batchable<sObject>, Database.Stateful
{
    global Database.QueryLocator start(Database.batchableContext bc)
    {
        String accQuery = 'Select Id, phone, fax,(select id, phone, fax from Contacts) from Account where Industry =  \'Construction\'';
        return Database.getQueryLocator(accQuery);
    }
           
    global void execute(Database.batchableContext bc, List<Account> scope)
    {
                  
                  List<Contact> conList = new List<Contact>();
                  
                  for(Account accObj : scope)
                  {
                     for(Contact conObj : accObj.contacts)
                     {
                      conObj .Phone = accObj.Phone;
                      conObj .Fax = accObj.Fax;
                      conList.add(conObj);
                     }
                  }
                  
                  if(conList.size()>0 && conList != null)
                     update conList;
    }

    global void finish(Database.BatchableContext bc)
	{
        
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[] {'pradeep.gunturi@gmail.com'};
               mail.setSubject('Match Merge Batch ');
            mail.setPlainTextBody('hi pradeep batchapex completed');
         mail.setToAddresses(toAddresses);
       List<Messaging.SendEmailResult> sendRes = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, true);
        
       
    }    

}
If you have any query please let me know.

If it solves your problem then mark as a best answer.

Thanks,
Arvind Kumar
 
This was selected as the best answer
sales@myvarmasales@myvarma
but i didnt get answer 
Arvind KumarArvind Kumar
You are facing any issue in batch class