I could not get reliable information about this problem on the Internet. But I think that this should be a problem that should affect many people.
Basically I wrote a simple trigger and test class in the sandbox, tested it, and when everything was ok, I deployed it in PRD.
At first I tried the verification mode and I got this error.
System.LimitException: too many SOQL queries: 101
This error was shown in some other testing class. Therefore, I feel that the test cases in my trigger are triggering, and this, in combination with the rest of the test cases, somehow exceeded the limit.
Thus, the total number of SOQL queries in our unit tests should be less than 100. Is this a little difficult to do right? I can imagine that we have so many test cases, of course, we will need more than 100 queries.
So, what are some ways to avoid this limitation since Salesforce runs all test cases when you deploy even a single line of code.
I have no ordinary suspects ... like SOQL in a for loop.
UPDATE: 8/19/2012: now I submit the source code of the test class and run
Testing Class:
@isTest
private class TestAccountDuplicateWebsiteTrigger {
static testMethod void myUnitTest() {
try{
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;
Test.stopTest();
}
catch (Exception e)
{
}
}
}
Trigger
trigger osv_unique_website_for_account on Account (before insert, before update) {
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account: System.Trigger.new)
{
if ((account.Website != null) && (System.Trigger.isInsert ||
(account.Website != System.Trigger.oldMap.get(account.Id).Website)))
{
if (accountMap.containsKey(account.Website))
{
account.Website.addError('Cannot save account. Website already exists.');
}
else
{
accountMap.put(account.Website, account);
}
}
}
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.');
}
}
}
Can you share your thoughts?
Thank,
Kelvin