我無法在線獲得有關此問題的任何可靠信息。但我認爲這肯定是一個影響很多人的問題。Salesforce:全面避免測試類中的管理員限制
基本上我在沙箱裏寫了一個簡單的觸發器和測試類,測試了它,當它很好的時候我把它部署到PRD。
我試着先驗證模式,我得到了這個錯誤。
System.LimitException:太多SOQL查詢:101
示於一些其它測試類發生此錯誤。所以我覺得我的觸發器中的測試用例已經運行,並且這與其餘的測試用例不知何故超出了限制。
因此,我們的單元測試中SOQL查詢的總數量必須少於100個。這有點難以正確對待?我可以想象有這麼多的測試用例,我們當然需要超過100個查詢。
那麼有什麼方法可以避免遇到這個限制,因爲Salesforce在部署甚至一行代碼時運行所有的測試用例。
我沒有任何通常的嫌疑人......就像在for循環中的SOQL一樣。
更新:2012年8月19日:我現在發佈的測試類的源代碼,並引發
測試類:
@isTest
私有類TestAccountDuplicateWebsiteTrigger {
static testMethod void myUnitTest() {
try{
// TO DO: implement unit test
Test.startTest();
Account a1;
a1 = new Account();
a1.name = 'GMSTest';
a1.Website = 'www.test.com';
Account a2;
a2 = new Account();
a2.name = 'GMSTest2';
a2.Website = 'www.test.com';
Account a3;
a3 = new Account();
a3.name = 'GMSTest3';
a3.Website = 'www.test1.com';
insert a1;
insert a2;
//insert a3;
Test.stopTest();
}
catch (Exception e)
{
}
}
}
觸發
trigger osv_unique_website_for_account on Account (before insert, before update) {
//Map which has no duplicates with website as the key
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account: System.Trigger.new)
{
//Ensure that during an update, if an website does not change - it should not be treated as a duplicate
if ((account.Website != null) && (System.Trigger.isInsert ||
(account.Website != System.Trigger.oldMap.get(account.Id).Website)))
{
//check for duplicates among the new accounts in case of a batch
if (accountMap.containsKey(account.Website))
{
account.Website.addError('Cannot save account. Website already exists.');
}
else
{
accountMap.put(account.Website, account);
}
}
}
//Now map containing new account websites has been created.
//Check them against the account websites that ALREADY EXIST in salesforce. If website exists, display error.
for (Account account : [SELECT Website FROM Account WHERE Website IN :accountMap.KeySet()])
{
Account newAccount = accountMap.get(Account.Website);
if (newAccount!=null)
{
newAccount.Website.addError('Cannot save account. Website already exists.');
}
}
}
能否請您分享您的想法?
感謝,
卡爾文
您收到此錯誤消息,因爲您的一個測試類(不是所有這些類)都超過了限速器限制。在錯誤消息中,您可以看到類名。發佈該類的代碼,以便我們可以查看它。 – mast0r 2012-08-17 17:59:41
實際上,所顯示的類是一個在PRD中的類,目前測試用例工作正常。正如約翰在下面回答的,也許是因爲我沒有使用startTest()和stopTest()把我的代碼放在測試類中。 – 2012-08-17 20:10:22