• RudrAbhishek
  • NEWBIE
  • 0 Points
  • Member since 2013
  • Software Engineer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies

I have created a Batch Apex in which under the finish method I am trying to launch an email to the corncerned user who is scheduling the job or executing the job / batch from the Developer console, mail is delivering instantly but not getting the START TIME & END TIME of the Scheduled Apex Job rest of the information such as NUMBER OF OPPORTUNITIES CLOSED WON TODAY & TOTAL OPPORTUNITY AMOUNT FOR TODAY:

 

global class OppAmountAgg implements Database.Batchable<sObject>, Database.Stateful{
global double totalAmount = 0;
String str = 'Closed Won';
global double totalSize = 0;
global Database.querylocator start(Database.BatchableContext BC){
  String query = 'SELECT StageName, Ex_Revenue__c, IsAggregated__c, Amount FROM Opportunity WHERE CreatedDate = today AND IsAggregated__c = false AND  StageName=\'' + str + '\'';
    return Database.getQueryLocator(query);
    }

global void execute(Database.BatchableContext BC, List<sObject> scope){
     List<Opportunity> opportunities = new List<Opportunity>();
  for(sObject sobjectIter : scope){
         Opportunity opportunity = (Opportunity)sobjectIter;
         opportunity.IsAggregated__c = true;
         if(opportunity.Amount != null){
          totalAmount = totalAmount+opportunity.Amount;
          opportunities.add(opportunity);
         }
        }
  totalSize = opportunities.size();
  update opportunities;
    }    

global void finish(Database.BatchableContext BC){
//CronTrigger ct = [SELECT Id, StartTime, EndTime, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id =:job.Id];
  String email = 'abc.def@ghi.com';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
//mail.setReplyTo('abc.def@ghi.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Daily Opportunity Amount Aggregator Job Complete');
system.debug('***CronTrigger.StartTime**' + CronTrigger.StartTime);
system.debug('***CronTrigger.EndTime**' + CronTrigger.EndTime);
mail.setPlainTextBody('Start DateTime: ' + CronTrigger.StartTime +' End DateTime: ' + CronTrigger.EndTime +' Number of Opportunities Closed Won Today: '+totalSize+' Total Opportunity Amount for today: '+totalAmount);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Could you please let me know that how we can also provide the StartTime and EndTime of the scheduled job to notify the user who's scheduling / executing the batch because as of now it's only sending Mail Body as follows:

"Start DateTime: StartTime End DateTime: EndTime Number of Opportunities Closed Won Today: 2.0 Total Opportunity Amount for today: 5750.0"

Hi All,

need to write a new trigger on lead which will check for ‘Open-Not Contacted’ Leads and then creates a duplicate entry of the same lead but with status as ‘Working – Contacted’, rest all information will be the same.

Thanks,

RudrAbhishek

Hello All,

Please help me out with suggesting that how i need to proceed with Apex Trigger for creating new task on successful creation of new Account.

 

Regards,

RudrAbhishek

I have created a Batch Apex in which under the finish method I am trying to launch an email to the corncerned user who is scheduling the job or executing the job / batch from the Developer console, mail is delivering instantly but not getting the START TIME & END TIME of the Scheduled Apex Job rest of the information such as NUMBER OF OPPORTUNITIES CLOSED WON TODAY & TOTAL OPPORTUNITY AMOUNT FOR TODAY:

 

global class OppAmountAgg implements Database.Batchable<sObject>, Database.Stateful{
global double totalAmount = 0;
String str = 'Closed Won';
global double totalSize = 0;
global Database.querylocator start(Database.BatchableContext BC){
  String query = 'SELECT StageName, Ex_Revenue__c, IsAggregated__c, Amount FROM Opportunity WHERE CreatedDate = today AND IsAggregated__c = false AND  StageName=\'' + str + '\'';
    return Database.getQueryLocator(query);
    }

global void execute(Database.BatchableContext BC, List<sObject> scope){
     List<Opportunity> opportunities = new List<Opportunity>();
  for(sObject sobjectIter : scope){
         Opportunity opportunity = (Opportunity)sobjectIter;
         opportunity.IsAggregated__c = true;
         if(opportunity.Amount != null){
          totalAmount = totalAmount+opportunity.Amount;
          opportunities.add(opportunity);
         }
        }
  totalSize = opportunities.size();
  update opportunities;
    }    

global void finish(Database.BatchableContext BC){
//CronTrigger ct = [SELECT Id, StartTime, EndTime, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id =:job.Id];
  String email = 'abc.def@ghi.com';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
//mail.setReplyTo('abc.def@ghi.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Daily Opportunity Amount Aggregator Job Complete');
system.debug('***CronTrigger.StartTime**' + CronTrigger.StartTime);
system.debug('***CronTrigger.EndTime**' + CronTrigger.EndTime);
mail.setPlainTextBody('Start DateTime: ' + CronTrigger.StartTime +' End DateTime: ' + CronTrigger.EndTime +' Number of Opportunities Closed Won Today: '+totalSize+' Total Opportunity Amount for today: '+totalAmount);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Could you please let me know that how we can also provide the StartTime and EndTime of the scheduled job to notify the user who's scheduling / executing the batch because as of now it's only sending Mail Body as follows:

"Start DateTime: StartTime End DateTime: EndTime Number of Opportunities Closed Won Today: 2.0 Total Opportunity Amount for today: 5750.0"