我已经写了一个Apex调度程序,在员工生日前2天向管理员发送电子邮件通知。Salesforce:调用方法并将联系人与帐户关联以获取顶点测试方法?
我已经在沙箱中进行了测试,并且已准备好转入生产。但是,我无法通过测试方法获取代码覆盖率。
我的Apex调度程序类使用SOQL语句来识别与帐户名称为“我们的公司”的联系人,并且在2天内有生日。
我在我的测试方法中创建了一个帐户和联系人记录。
我需要将我的帐户和联系人记录链接/关联在一起。 (联系人为我创建的帐户工作,我知道它与ID有关,你不能写帐户ID
我想我需要创建一个帐户变量并将其分配给联系ID ...我已经试过代码示例,但他们没有工作的Salesforce认为我想写的接触ID,你不能做的是具体的:。
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
AccountID = testAccount.id;
如何创建一个帐户可变
?另一个问题是拨打我的sendBirthdayEmail();
方法。
写作testContact.sendBirthdayEmail();不打电话给我的电子邮件方法。相反,我收到错误消息“错误:编译错误:SObject联系人的无效字段ContactID”。
为什么我的测试方法不能识别我的sendBirthdayEmail方法?测试方法不了解我班的其他方法吗?
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();
}
}
请帮助我,我已经阅读文档,并搜查了留言板,但我仍然坚持。谢谢你的帮助!
谢谢......解决了我的问题! – user1529039 2012-08-02 19:33:09
没问题!随时将我的答案标记为已接受。 :) – Anup 2012-08-02 19:48:33