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
RohanSharma8791RohanSharma8791 

Method does not exist or incorrect signature: void daysbetween(Datetime) from the type Datetime

I am getting this error in my code.

I want to write a Batch Class that will automatically sends an EMAIL to the account in which there is a total number of contacts created in last 7 days realated to that account on weekly basis.

my code is below
 
public class batchEmailProcess implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        String stringofRecords = ('SELECT Id,Name,Email__c,(Select Id,Name, CreatedDate From Contacts) FROM Account');
        return Database.getQueryLocator(stringofRecords);
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        {
            Date D = date.today(); 
            DateTime DT = Datetime.newInstance(D.year(), D.month(),D.day(),0,0,0); 
            integer i;
            List<String> sendTo = new List<String>();
            for(Account acc :records){
                String s ='';
                for(Contact Con :acc.contacts){ 
                    if(con.CreatedDate.daysbetween(DT)<=7){
                        sendTO.add(acc.Email__c);
                        i+=1;
                        s= acc.Name+'Contacts Created'+i+ ',';
                    }
                }
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setSenderDisplayName('Email Alert');
                String body = 'Account Related Contacts==> '+s.removeEnd(',');
                mail.setSubject('Account Related Contacts');
                mail.setToAddresses(sendTo);
                mail.setHtmlBody(body);
                mails.add(mail);
                acc.Description =  s.removeEnd(',');
            }
            Messaging.SendEmail(mails);
        }
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}


I am getting the following error "Method does not exist or incorrect signature: void daysbetween(Datetime) from the type Datetime"

Can someone please rectify the code
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Rohan,

The function only accepts the parameters as Date fields. But I guess the Created Date is Datetime field . Can you please refer the below article for the same.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_date.htm#apex_System_Date_daysBetween

If this solution helps, Please mark it as best answer.

Thanks,