You need to sign in to do that
Don't have an account?

trigger with (To mant SOQL Queiries) in Test case
/
Trigger code
trigger AddOwnerColor on Account (before insert, before update) {
// create a set of all the unique ownerIds
Set<Id> ownerIds = new Set<Id>();
for (Account a : Trigger.new)
ownerIds.add(a.OwnerId);
// query for all the User records for the unique userIds in the records
// create a map for a lookup / hash table for the user info
Map<Id, User> owners = new Map<Id, User>([Select UserFavColor__c from User Where Id in :ownerIds]);
// iterate over the list of records being processed in the trigger and
// set the color before being inserted or updated
for (Account a : Trigger.new)
a.Owner_Favorite_Color__c = owners.get(a.OwnerId).UserFavColor__c;
}
########################Test Classes##############################
@isTest
private class TestAccountColorTrigger {
static testMethod void testBulkInsert() {
List<Account> accounts = new List<Account>();
Profile p = [select id from profile where name='Marketing User'];
// create a user to run the test as
User u = new User(alias = 'test123', email='test1234@noemail.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
UserFavColor__c='Buttercup Yellow',
timezonesidkey='America/Los_Angeles', username='test1234@noemail.com');
insert u;
Profile p1 = [select id from profile where name='Standard User'];
// create a user to own the account
User u1 = new User(alias = 'test123', email='test12345@noemail.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p1.Id, country='United States',
UserFavColor__c='Pretty Pink',
timezonesidkey='America/Los_Angeles', username='test12354@noemail.com');
insert u1;
// add 200 accounts to the list to be inserted
for (Integer i=0;i<200;i++) {
Account a = new Account(
Name = 'Test Account',
OwnerId = u1.Id
);
accounts.add(a);
}
// Switch to the runtime context
Test.startTest();
// run as a different user to test security and rights
System.runAs(u) {
insert accounts;
}
// Switch back to the original context
Test.stopTest();
// query for all accounts created and assert that the color was added correctly
for (Account acct : [Select Owner_Favorite_Color__c from Account Where OwnerId = :u1.Id])
System.assertEquals(acct.Owner_Favorite_Color__c,'Pretty Pink');
}
}
may be that you have other trigers on account which contains soql statements.... and cnsequently all together goes beyond limit
Hi
There is only one trigger in Account.
Can you please help me out .
Regards
Ashok
update the for loop inside the test class.
Replace :
// query for all accounts created and assert that the color was added correctly
for (Account acct : [Select Owner_Favorite_Color__c from Account Where OwnerId = :u1.Id])
System.assertEquals(acct.Owner_Favorite_Color__c,'Pretty Pink');
}
with
List<Account> accounts = [Select Id, Name, OwnerId ,Owner_Favorite_Color__c from Account Where OwnerId = :u1.Id]
for (Account acct : accounts)
System.assertEquals(acct.Owner_Favorite_Color__c,'Pretty Pink');
}];
Just check for any SOQL queries within for loop, this may cause the error you mentioned.
Thanks,
Salesforce knowledge sharing and Learn by examples @
CloudForce4u