I have written an Apex Scheduler that sends a manager an email notification 2 days before an employees birthday.
I have tested it in sandbox and I'm ready to move to production. However, I'm having trouble getting code coverage with my test method.
My Apex Scheduler Class uses a SOQL statement to identify a contact with the Account name 'Our Company' AND that has a Birthday in 2 days.
I have created an account and contact record in my test method.
I need to link/associate my account and contact record together. (The contact works for the account I created. I know it has something to do with ID. You can't write the accountId.
I thinkI need to create an account variable and assign that to the contactID...I've tried code examples but they aren't working. Salesforce thinks I'm trying to write the contact ID which you can't do. To be Specific:
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
AccountID = testAccount.id;
How do I create an account variable?
Another issue is calling my sendBirthdayEmail();
method.
writting testContact.sendBirthdayEmail(); doesn't call my email method. Instead I get an error message "Error: Compile Error: Invalid field ContactID for SObject Contact".
Why doesn't my test method recognize my sendBirthdayEmail method? Do test methods not understand the other methods in my class?
global class BirthdayName implements Schedulable{
global void execute (SchedulableContext ctx)
{
sendBirthdayEmail();
}
public void sendBirthdayEmail()
{
for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Our Company'])
{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId('005J0000000JWYx');
mail.setsubject('Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}
static testMethod void myTestBirthday() {
//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Our Company and has a birthday 2 days away
Account testAccount = new Account(name='Our Company');
insert testAccount;
// creating new contact
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
// TRYING to make this contact associated with the account by setting the account id of the account I just created to the contact
AccountID = testAccount.id;
// inserting test contact
insert testContact;
testContact.birthdate=system.Today().addDays(2);
update testContact;
testContact.sendBirthdayEmail();
}
}
Please help me, I have read the documentation and searched the message boards but I'm still stuck. Thank you for all your help!