2012-08-01 27 views
0

我已經寫了一個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(); 


} 



} 

請幫助我,我已經閱讀文檔,並搜查了留言板,但我仍然堅持。謝謝你的幫助!

回答

1

嘗試分配testAccount.Id到testContact.Account:

testContact.account = testAccount.Id; 

代替 的AccountID = testAccount.Id;

希望這會有所幫助!

Anup

+0

謝謝......解決了我的問題! – user1529039 2012-08-02 19:33:09

+0

沒問題!隨時將我的答案標記爲已接受。 :) – Anup 2012-08-02 19:48:33

相關問題