• Pawani Mutturu
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
global class BatchApex implements Database.Batchable<sObject>{
    
    String query = 'SELECT Id,CLOSED__c, Priority FROM Case';
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Case> cas){
        for(Case c : cas){
            if(c.priority=='High'){
                c.CLOSED__c = TRUE;           
            }
        }
        update cas;
        for(Case cs:cas){
            if(cs.Business_days__c==5){
                cas.add(cs);
                sendmail(); 
            }
        }
        update cas; 
    }
    
    global void finish(Database.BatchableContext BC){     
        
    }    
    
    public void sendmail()
    {
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'iswarya.sekar@excelenciaconsulting.com'} ;
            email.setToAddresses(toAddresses) ;
        email.setPlainTextBody('this is a test mail');
        email.setSubject('New Case Logged');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
    }
}
Its updating CLOSED__c to True, But it is not sending email when Business_days__c==5
 

I need custom picklist fields on Account to be autopopulated on creating New Account. I have written trigger for this. It is working on before update but not working for Before insert. Please help.
User-added image
global class UpdateContactAdresses implements Database.Batchable<sObject>, Database.stateful

{
    global Integer recordsProcessed = 0;
    
    global Database.QueryLocator start(Database.BatchableContext bc)
        
    {
        return.Database.getQueryLocator('select id, BillingStreet, BillingCity, BillingState, '+'BillingPostalCode, (select id, MailingStreet, MailingCity,'+' MailingState, MailingPostalCode from contacts) from Account'+'where BillingCountry=\'USA\'');
        
    }
    
    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.MailingStreet = account.BillingStreet;
                contact.MailingState = account.BillingState;
                contact.MailingCity = account.BillingCity;
                contact.MailingPostalCode = account.BillingPostalCode;
        
                contacts.add(contact);
                recordsProcessed = recordsProcessed + 1;
            } 
                
                
        }
        
        update contacts;
        
    }        
        global void finish(Database.BatchableContext bc)
        {       
         system.debug(recordProcessed +' record is processed');
         AsyncApexJob job = [select id, status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where id=: bc.getJobId()];
         EmailUtils.SendMessage(a, recordsProcessed);
        }
        }
    
        
global class UpdateContactAdresses implements Database.Batchable<sObject>, Database.stateful

{
    global Integer recordsProcessed = 0;
    
    global Database.QueryLocator start(Database.BatchableContext bc)
        
    {
        return.Database.getQueryLocator('select id, BillingStreet, BillingCity, BillingState, '+'BillingPostalCode, (select id, MailingStreet, MailingCity,'+' MailingState, MailingPostalCode from contacts) from Account'+'where BillingCountry=\'USA\'');
        
    }
    
    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.MailingStreet = account.BillingStreet;
                contact.MailingState = account.BillingState;
                contact.MailingCity = account.BillingCity;
                contact.MailingPostalCode = account.BillingPostalCode;
        
                contacts.add(contact);
                recordsProcessed = recordsProcessed + 1;
            } 
                
                
        }
        
        update contacts;
        
    }        
        global void finish(Database.BatchableContext bc)
        {       
         system.debug(recordProcessed +' record is processed');
         AsyncApexJob job = [select id, status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where id=: bc.getJobId()];
         EmailUtils.SendMessage(a, recordsProcessed);
        }
        }