+ Start a Discussion
sonam gupthasonam guptha 

Grabing the accounts even more than 60 days even


iam trying to send an email notification based on custom column,so here i have to send an email when my EndDate__c is less than 60 days,the below code is working fine but its grabing few accounts even more than 60 days,can i get any help to fix it?

i think iam not calculating right value here?
if(m.EndDate__c.daysBetween(D) < 60 )
global class expireNotify implements Database.Batchable<sObject> {
   global Database.QueryLocator start(Database.BatchableContext bc) {
       //Date d = Date.today()+90;
       String soql = 'SELECT EndDate__c, Name FROM Account WHERE EndDate__c !=NULL';
       return Database.getQueryLocator(soql);
   global void execute(Database.BatchableContext bc, List<account> recs) {
       List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
       Date D =Date.today();
                  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    List<String> toAddresses = new List<String>();
                    String messageBody;
                    list<string> accstring =new list<string>();
                    integer i=0;
       for(account m : recs) {
           if(m.EndDate__c.daysBetween(D) < 60 )
            messageBody = i+'-->'+ m.Name ;
            accstring.add(messageBody );           
           mail.setSubject('Welcome to Sweet 16 Siebel Batch');
           string allstring = string.join(accstring,'<br/>');
       mail.setHtmlBody('<html><body>Hi Sir Hope you are well <br>'+'<br> List of accounts <br>'+allstring + ',<br>Your accounts Expires today. <br>Kindly contact your administrator.<br><br><b>Regards,</b><br>Magulan D<br/></body></html>'); 


   global void finish(Database.BatchableContext bc) {

1. If its capturing some more accounts just due to hours diff, try  using GMT timings for exact comparision.
2. It would make more sense to query for 60 days filter in SOQL itself.

Let me know if its retreiving records with diff or more than 61 days or so.
sonam gupthasonam guptha
how we can write that soql query,its not accepting as below
String soql = 'SELECT EndDate__c, Name FROM Account WHERE EndDate__c !=NULL'  AND EndDate__c < 60;

SELECT Name FROM Account WHERE Createddate !=NULL  AND Createddate < LAST_N_DAYS:60

Replace your columns accordingly !
sonam gupthasonam guptha
But we need EndDate__c info right?that gives account createddate info
I just gave an example...replace cretedate with your enddate column name,..
sonam gupthasonam guptha
it gaves full accounts which created less than 60 days,here i need to calculate like if today() -enddate__c = 60 days then collect accounts and send in email
ok .for that..you can use this in your start block:
Date d = System.today() - 60;
List<Account> a = [SELECT Name,EndDate__c  FROM Account WHERE  EndDate__c =:d];

sonam gupthasonam guptha
now its only generarting the accounts created on that particular day,means 60 days - today() ,which results accounts created on month of 5(may) -01(day) created accounts
so thats what you want right ?? my above query will give records as of enddate = 60 days before today and the one I gave earlier will all within 60 days. You can use > or < to get the exact data in the first query I gave.
Varun SinghVarun Singh
Hi Sonam,

you can use 
Select id,name  from account where  enddate__c= LAST_N_DAYS:60

I Checkked in developer console its working fine

User-added image