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
nikkeynikkey 

BATCH APEX ERROR in CODE

Hi Friends ,

Can anyone help me in resolving this code .

Error: Compile Error: Method does not exist or incorrect signature: [String].day() at line 19 column 37

 

// memberBirthdayBatch:

global class memberBirthdayBatch implements Database.batchable<Member__c>
{
global Iterable<Member__c> start(Database.batchableContext info)
{
System.debug('Start method');
return new callMemberIterable();
}
global void execute(Database.batchableContext info, List<Member__c> scope)
{
List<Member__c> memsToUpdate = new List<Member__c>();
System.debug('Member list size is ' + scope.size());
for(Member__c m : scope)
{
Date myDate = date.today();
Integer todayDy = myDate.day();
Integer todayMon = myDate.month();
System.debug('Day is ' + m.BirthDay__c.day());
Integer dy = m.BirthDay__c.day();
Integer mon = m.BirthDay__c.month();
if(todayDy == dy && todayMon == mon)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(m.EmailAddress__c);
email.setToAddresses(toAddresses);
List<String> ccAddresses = new List<String>();
ccAddresses.add('salesforcecrm@gmail.com');
email.setCcAddresses(ccAddresses);
email.setSubject('Happy Birthday. Have a blast -- Birthday Reminder!');
String message = '<html><table cellspacing = "7"><tr><td style="font-weight:bold;color:green;">Happy Birthday!!!</td></tr><tr><td style="font-weight:bold;color:pink;">Many more Happy returns of the day.</td></tr><tr><td></td></tr><tr><td></td></tr><tr><td style="font-weight:bold;">Cheers,</td></tr><tr><td style="font-weight:bold;">XYZ</td></tr></table></html>';
email.setHtmlBody(message);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
}
global void finish(Database.batchableContext info)
{
}
}

 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
@nikkey - Seems like your field BirthDay__c on Member__c object is a text field instead of date. One way to fix this would be to change this to a date (please note all implications with regards to existing data). Another way would be to parse this string based on locale for e.g. your dates are stored as mm/dd/yyyy try m.BirthDay__csplit('/')[0] to get the day and m.BirthDay__csplit('/')[1] to get the month. Another alternative would be to convert the data in the Birthday__c field to date and call the day() and month functions on this.

All Answers

KaityKaity

Can you please try this:

 

// retreive today's date
Date todayDate = Date.valueOf(System.today());

// retreives today's date and month separately
Integer day_today = todayDate.day();
Integer month_today = todayDate.month();

 

-Kaity

vbsvbs
@nikkey - Seems like your field BirthDay__c on Member__c object is a text field instead of date. One way to fix this would be to change this to a date (please note all implications with regards to existing data). Another way would be to parse this string based on locale for e.g. your dates are stored as mm/dd/yyyy try m.BirthDay__csplit('/')[0] to get the day and m.BirthDay__csplit('/')[1] to get the month. Another alternative would be to convert the data in the Birthday__c field to date and call the day() and month functions on this.
This was selected as the best answer
sfdcfoxsfdcfox
vbs, my sentiments exactly. Of course, they could also just Date.parse(m.birthday__c).day(), assuming that birthday had a valid date.
nikkeynikkey

Hi Friends ,

Thanks a lot for ur help.

 

I have changed my field BirthDay__c on Member__c object to Date data type .