You need to sign in to do that
Don't have an account?
Ramesh Guduru
Too many SOQL queries: 101 -System.LimitException:
I am new to salesforce,could you please assist me,i am getting errror while running these below test class.
(error message :-System.LimitException: Too many SOQL queries: 101)
class name:-UpdateAccounts
test class name:-TestUpdateAccounts
i have attached both class & test class
PFA.
Thanks
(error message :-System.LimitException: Too many SOQL queries: 101)
class name:-UpdateAccounts
test class name:-TestUpdateAccounts
i have attached both class & test class
PFA.
Thanks
class name:-UpdateAccounts ************************** /*Class Name: UpdateAccounts.cls Original Author: Prathyusha Uppada, NRC Original Creation Date: 11/16/2010 Original Release: 2.3 Purpose: The class methods are invoked by the AccountMasterTrigger.trigger. Whenever the Ultimate_Parent_Client__c of a subsidiary Client or the Type of an Ultimate Parent Client is updated the TYPE of all the subsidiary clients should be updated to its Ultimate Parent's TYPE. Ticket#: 207999 Changes: PU: 01/19/2011:Defect# 218746- Udpated the updateAccountType method to be invoked by the AccountMasterTrigger.trigger on after insert as well. PU: 03/04/2011:Ticket#219268- When the Sub-Type of an Ultimate parent is updated the sub-type on the corresponding child clients should be updated as well. Ticket#: 251052 Enhancment - Updated the code so that Strategic client start date and end date will be cascade in the hierarchy. */ public with sharing class UpdateAccounts { /* Input: We pass in the new and old values of the trigger. Process: Retrieve all the Subsidary clients whose UltimateParent or its ultimateParent's Type has changed and update the Type Output: N/A Changes: */ public static void updateAccountType(Map<Id,Account> mapOfNewAccounts,Map<Id,Account> mapOfOldAccounts,List<Account> accsList) { Set<Id> ultimateParentIds = new Set<Id>(); Set<Id> subsidiaryClientUPIds= new Set<Id>(); Set<Id> subsidiaryClientIds= new Set<Id>(); Map<Id,Account> mapOfUParents; List<Account> accsToUpdate = new List<Account>(); Map<Id,Integer> mapOfaccsToUpdate = new Map<Id,Integer>(); boolean isFlagged = true; Integer i=0; //Flag is validated to ensure that the trigger is executed only once if(isFlagged == true) { //Loop through the list of Accounts being updated and add all the ultimate parent Clients to ultimateParentIds List // and all the subsidiaryClient Ids and their UltimateParent Ids to subsidiaryClientIds and subsidiaryClientUPIds respectively for(Account a : accsList) { //PU: 01/19/11- Updated the if condition to execute the below code only when this method is invoked as part of the update trigger, //Since we need to update the Type of all child clients only when the Type of its UP is changed and not when a new Uparent is created. if((a.Ultimate_Parent__c == true)&& (mapOfOldAccounts!=null)) { if((mapOfOldAccounts.get(a.ID).Type != mapOfNewAccounts.get(a.ID).Type) || (mapOfOldAccounts.get(a.ID).Strategic_Client_Start_Date__c != mapOfNewAccounts.get(a.ID).Strategic_Client_Start_Date__c ) || (mapOfOldAccounts.get(a.ID).Strategic_Client_End_Date__c!= mapOfNewAccounts.get(a.ID).Strategic_Client_End_Date__c ) || (mapOfOldAccounts.get(a.ID).MP_US_National_Accounts__c != mapOfNewAccounts.get(a.ID).MP_US_National_Accounts__c)) { //List of UltimateParents whose Type has changed. ultimateParentIds.add(a.ID); } } else { //List of Subsidiary Clients and its corresponding UltimateParentids subsidiaryClientIds.add(a.Id); subsidiaryClientUPIds.add(a.Ultimate_Parent_Client__c); } } //Retrieve all the Subsidiary clients for the UltimateParents whose Type has changed // and add them to the accsToUpdate list if(ultimateParentIds.size() > 0) { for(Account acc1 : [Select Type,MP_US_National_Accounts__c,Strategic_Client_Start_Date__c ,Strategic_Client_End_Date__c, Id , Ultimate_Parent_Client__c,Ultimate_Parent__c From Account Where Ultimate_Parent_Client__c in: ultimateParentIds ]) { // Check to see that the client has not been added to the accsToUpdate list if(!mapOfaccsToUpdate.ContainsKey(acc1.Id)) { acc1.Type = mapOfNewAccounts.get(acc1.Ultimate_Parent_Client__c).Type; acc1.Strategic_Client_Start_Date__c = mapOfNewAccounts.get(acc1.Ultimate_Parent_Client__c).Strategic_Client_Start_Date__c; acc1.Strategic_Client_End_Date__c = mapOfNewAccounts.get(acc1.Ultimate_Parent_Client__c).Strategic_Client_End_Date__c; acc1.MP_US_National_Accounts__c = mapOfNewAccounts.get(acc1.Ultimate_Parent_Client__c).MP_US_National_Accounts__c; accsToUpdate.add(acc1); mapOfaccsToUpdate.put(acc1.Id,i+1); } } } if(subsidiaryClientUPIds.size() > 0) { //Retrieve UltimateParentids for all the Subsidiary clients whose Ultimate_parent_client__c has changed mapOfUParents = new Map<Id,Account>([Select Id ,MP_US_National_Accounts__c,Sub_Type__c,Strategic_Client_End_Date__c,Strategic_Client_Start_Date__c, Type From Account Where Id in: subsidiaryClientUPIds ]); //Iterate over all the Subsidiary clientsand update the Type field for(Account acc2 : [Select Id,Sub_type__c,MP_US_National_Accounts__c,Strategic_Client_End_Date__c,Strategic_Client_Start_Date__c,Ultimate_Parent_Client__c,Type From Account Where Id in: subsidiaryClientIds]) { // Update the Sub_Type__c field for all the subsidiary clients that satisfies //the below condition if((!mapOfaccsToUpdate.containsKey(acc2.Id))&& (mapOfUParents.containsKey(acc2.Ultimate_Parent_Client__c))&& (acc2.Type != mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Type || (acc2.Strategic_Client_Start_Date__c != null && mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_Start_Date__c != null && acc2.Strategic_Client_Start_Date__c != mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_Start_Date__c ) || (acc2.Strategic_Client_End_Date__c != null && mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_End_Date__c != null && acc2.Strategic_Client_End_Date__c != mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_End_Date__c ) || (acc2.MP_US_National_Accounts__c != null && mapOfUParents.get(acc2.Ultimate_Parent_Client__c).MP_US_National_Accounts__c != null && acc2.MP_US_National_Accounts__c != mapOfUParents.get(acc2.Ultimate_Parent_Client__c).MP_US_National_Accounts__c ) )) { acc2.type = mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Type; acc2.Strategic_Client_Start_Date__c = mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_Start_Date__c ; acc2.Strategic_Client_End_Date__c = mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Strategic_Client_End_Date__c ; acc2.MP_US_National_Accounts__c = mapOfUParents.get(acc2.Ultimate_Parent_Client__c).MP_US_National_Accounts__c ; accsToUpdate.add(acc2); mapOfaccsToUpdate.put(acc2.Id,i+1); } } } // Update the list of clients if(accsToUpdate.size()> 0) try{ update accsToUpdate; } catch(exception e){ System.debug(''); } isFlagged = false; } } /* Input: We pass in the new and old values of the trigger. Process: Retrieve all the Subsidary clients whose UltimateParent or its ultimateParent's Type has changed and update the Sub-Type Output: N/A Changes: */ public static void updateAccountSubType(Map<Id,Account> mapOfNewAccounts,Map<Id,Account> mapOfOldAccounts,List<Account> accsList) { Set<Id> ultimateParentIds = new Set<Id>(); Set<Id> subsidiaryClientUPIds= new Set<Id>(); Set<Id> subsidiaryClientIds= new Set<Id>(); Map<Id,Account> mapOfUParents; List<Account> accsToUpdate = new List<Account>(); Map<Id,Integer> mapOfaccsToUpdate = new Map<Id,Integer>(); boolean isFlagged = true; Integer i=0; //Flag is validated to ensure that the trigger is executed only once if(isFlagged == true) { //Loop through the list of Accounts being updated and add all the ultimate parent Clients to ultimateParentIds List // and all the subsidiaryClient Ids and their UltimateParent Ids to subsidiaryClientIds and subsidiaryClientUPIds respectively for(Account a : accsList) { //Update the Sub_Type__c of all child clients only when the Sub_Type__c of its UP is changed and not when a new Uparent is created. if((a.Ultimate_Parent__c == true)&& (mapOfOldAccounts!=null)) { if(mapOfOldAccounts.get(a.ID).Sub_Type__c != mapOfNewAccounts.get(a.ID).Sub_Type__c) { //List of UltimateParents whose Sub_Type__c has changed. ultimateParentIds.add(a.ID); } } else { //List of Subsidiary Clients and its corresponding UltimateParentids subsidiaryClientIds.add(a.Id); subsidiaryClientUPIds.add(a.Ultimate_Parent_Client__c); } } //Retrieve all the Subsidiary clients for the UltimateParents whose Sub_Type__c has changed // and add them to the accsToUpdate list if(ultimateParentIds.size() > 0) { for(Account acc1 : [Select Sub_Type__c, Id , Ultimate_Parent_Client__c,Ultimate_Parent__c From Account Where Ultimate_Parent_Client__c in: ultimateParentIds ]) { // Check to see that the client has not been added to the accsToUpdate list if(!mapOfaccsToUpdate.ContainsKey(acc1.Id)) { acc1.Sub_Type__c = mapOfNewAccounts.get(acc1.Ultimate_Parent_Client__c).Sub_Type__c; accsToUpdate.add(acc1); mapOfaccsToUpdate.put(acc1.Id,i+1); } } } if(subsidiaryClientUPIds.size() > 0) { //Retrieve UltimateParentids for all the Subsidiary clients whose Ultimate_parent_client__c has changed mapOfUParents = new Map<Id,Account>([Select Id , Sub_Type__c From Account Where Id in: subsidiaryClientUPIds ]); //Iterate over all the Subsidiary clientsand update the Sub_Type__c field for(Account acc2 : [Select Id,Ultimate_Parent_Client__c,Sub_Type__c From Account Where Id in: subsidiaryClientIds ]) { // Update the Sub_Type__c field for all the subsidiary clients that satisfies //the below condition if((!mapOfaccsToUpdate.containsKey(acc2.Id))&& (mapOfUParents.containsKey(acc2.Ultimate_Parent_Client__c))&& (acc2.Sub_Type__c != mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Sub_Type__c)) { acc2.Sub_Type__c = mapOfUParents.get(acc2.Ultimate_Parent_Client__c).Sub_Type__c; accsToUpdate.add(acc2); mapOfaccsToUpdate.put(acc2.Id,i+1); } } } // Update the list of clients if(accsToUpdate.size()> 0) try{ update accsToUpdate; } catch(exception e){ System.debug(''); } isFlagged = false; } } } ******************** ***************************** test class name:-TestUpdateAccounts *************************** /**Class Name: TestUpdateAccounts.cls Original Author: Prathyusha Uppada, NRC Original Creation Date: 11/16/2010 Original Release: 2.3 Purpose: TestClass for UpdateAccounts.cls Ticket#: 207999 Changes: PU: 01/19/2011:Defect# 218746- Modified the test method childTypeNotEqualsUParentTypeOnInsert so that the AccountMasterTrigger is fired on after insert as well. Before this change in the test data since I am not populating the Client_short_name__c there is a work flow to update the Client shortname which triggers the AccountMasterTrigger on after Update, but if the ClientShortName is pre-populated,the workflow does not update the Clientshortname field resulting in not firing the after update trigger. PU: 03/04/2011:Ticket#219268- When the Sub-Type of an Ultimate parent is updated the sub-type on the corresponding child clients should be updated as well. */ @isTest private class TestUpdateAccounts { /* Input: No input variables Process: 1. Inserted 2 Ultimate Parent Accounts whose Type is 'Other Key Account' and 40 child Accounts whose Ultimate Parent Client is UParent 1. 2. Then we make the following modifications: - Modify the Type of UParent1 to Global Account. - Modify the Ultimate_Parent_Client of the first 20 child clients to UParent2. 3. On Update AccountMasterTrigger.trigger is fired. Output: Validate the Type field on Clients. For the first 20 clients The TYPE should be 'Other Key Account' and the next 20 clients the Type should be 'Global Account' Changes: */ static testMethod void bulkUpdateUPTypeAndUPClient() { List<Account> childAcclist = new List<Account>(); List<Account> ultimateAccountList = new List<Account>(); // Insert 2 Ultimate Parent Accounts whose Type is 'Other Key Account' for(Integer i=0;i<2;i++) { Account uacc= new Account(); uacc.Name='TestUpdateAccounts ultimateAccount'+i; uacc.BillingStreet='TestUpdateAccounts'+i+'Street'; uacc.Client_Short_Name__c ='CTypeUp'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.phone='111111'; uacc.BillingCountry='US'; uacc.BillingCity='TestUpdateAccounts'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Other Key Account'; ultimateAccountList.add(uacc); } insert ultimateAccountList; // Insert child Accounts whose UParent is Uparent1 for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChanged Child'+i; acc.BillingStreet='UPTypeAndUPClientIsChanged Child Street' + i; acc.Client_Short_Name__c ='test1'; acc.BillingState='WI'; acc.phone='111111'; acc.BillingPostalCode='5322'+i; acc.BillingCountry='US'; acc.BillingCity='UPTypeAndUPClientIsChanged City'+i; acc.Ultimate_Parent_Client__c = ultimateAccountList[0].Id; childAcclist.add(acc); } test.startTest(); insert childAcclist; test.stopTest(); List<Account> updateClients = new List<Account>(); //Modify the Type of UParent1 to Global Account and add them to updateClients List. Account updateUPAccount = [SELECT TYPE FROM Account WHERE ID =: ultimateAccountList[0].Id ORDER BY ID]; updateUPAccount.TYPE = 'Global Account'; updateClients.add(updateUPAccount); //Modify the Ultimate_Parent_Client of the first 20 child clients to UParent2 //and add them to updateClients List. for(Account a : [SELECT ID,TYPE,Ultimate_Parent_Client__c FROM Account WHERE ID =: childAcclist ORDER BY ID LIMIT 20] ) { a.Ultimate_Parent_Client__c = ultimateAccountList[1].Id; updateClients.add(a); } //Update the Accounts List update updateClients; //Retrieve all the child accounts and verify the Type // List<Account> accList = [SELECT TYPE FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=0;i<20;i++) System.assertEquals(updateClients[i].Type,'Other Key Account'); for(Integer i=20;i<40;i++) System.assertEquals(updateClients[i].Type,'Global Account'); } /* Input: No input variables Process: 1. Insert an Ultimate Parent Account whose Type is 'Other Key Account' 2.Insert 40 child Accounts whose Ultimate Parent Account equals the above UP and Type is 'Strategic Account' Output: Validate the Type field on Clients. The TYPE of the child accounts should be 'Other Key Account'. Changes: PU 01/19/11 - Updated the test data to populate the ClientshortName field. */ static testMethod void childTypeNotEqualsUParentTypeOnInsert() { List<Account> childAcclist = new List<Account>(); // Insert an Ultimate Parent Accounts whose Type is 'Other Key Account' Account uacc= new Account(); uacc.Name='childTypeNEUParentTypeOnInsert'; uacc.BillingStreet='childTypeNEUParentTypeOnInsertStreet'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.phone='111111'; uacc.BillingCountry='US'; uacc.BillingCity='childTypeNEUParentTypeOnInsertCity'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Other Key Account'; insert uacc; test.startTest(); // Insert child Accounts whose UParent is uacc for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChangedChild'+i; acc.Client_Short_Name__c =('UPTypeAndUPClientIsChangedChild'+i).substring(0,8); acc.BillingStreet='UPTypeAndUPClientIsChangedChildStreet' + i; acc.BillingState='WI'; acc.BillingPostalCode='5322'+i; acc.BillingCountry='US'; acc.phone='111111'; acc.BillingCity='UPTypeAndUPClientIsChangedCity'+i; acc.Ultimate_Parent_Client__c = uacc.Id; acc.Type = 'Strategic Account'; acc.Sub_Type__c ='Corporate'; childAcclist.add(acc); } insert childAcclist; // List<Account> accList = [SELECT TYPE FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=20;i<40;i++) System.assertEquals(childAcclist[i].Type,'Other Key Account'); test.stopTest(); } /* Input: No input variables Process: 1. Insert an Ultimate Parent Account whose Type is 'Other Key Account' 2.Insert 40 child Accounts whose Type is 'Strategic Account'and Ultimate Parent Account equals the above UP Output: Validate the Type field on Clients. The TYPE of the child accounts should be 'Other Key Account'. Changes: */ static testMethod void childTypeNotEqualsUParentTypeOnUpdate() { List<Account> childAcclist = new List<Account>(); List<Account> ultimateAccountList = new List<Account>(); // Insert an Ultimate Parent Accounts whose Type is 'Other Key Account' Account uacc= new Account(); uacc.Name='childTypeNEUParentTypeOnUpdate'; uacc.BillingStreet='childTypeNEUParentTypeOnUpdateStreet'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.phone='111111'; uacc.BillingCountry='US'; uacc.BillingCity='childTypeNEUParentTypeOnUpdateCity'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Other Key Account'; insert uacc; // Insert child Accounts whose UParent is uacc.ID for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChanged Child'+i; acc.BillingStreet='UPTypeAndUPClientIsChangedChildStreet' + i; acc.BillingState='WI'; acc.phone='111111'; acc.BillingPostalCode='5322'+i; acc.BillingCountry='US'; acc.BillingCity='UPTypeAndUPClientIsChanged City'+i; acc.Ultimate_Parent_Client__c =uacc.Id; childAcclist.add(acc); } test.startTest(); insert childAcclist; List<Account> updateClients = new List<Account>(); //Modify the Type from Other Key Account to Strategic Account' //and update the updateClients List. for(Account a : [SELECT ID,TYPE,Ultimate_Parent_Client__c FROM Account WHERE ID =: childAcclist ORDER BY ID LIMIT 20] ) { a.Type = 'Strategic Account'; a.Sub_Type__c = 'National'; updateClients.add(a); } update updateClients; //Retrieve all the child accounts and verify the Type // List<Account> accList = [SELECT TYPE FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=0;i<40;i++) System.assertEquals(updateClients[i].Type,'Other Key Account'); test.stopTest(); } /* Input: No input variables Process: 1. Inserted 2 Ultimate Parent Accounts whose Type is 'Strategic' and SubType is "Corporate" and 40 child Accounts whose Ultimate Parent Client is UParent 1. 2. Then we make the following modifications: - Modify the sub-Type of UParent1 to "International". - Modify the Ultimate_Parent_Client of the first 20 child clients to UParent2. 3. On Update AccountMasterTrigger.trigger is fired. Output: Validate the Type field on Clients. For the first 20 clients The SUB-TYPE should be 'Corporate' and the next 20 clients the Sub-Type should be 'International' Changes: */ static testMethod void bulkUpdateUPSubTypeAndUPClient() { List<Account> childAcclist = new List<Account>(); List<Account> ultimateAccountList = new List<Account>(); // Insert 2 Ultimate Parent Accounts whose Sub-Type is 'Corporate' for(Integer i=0;i<2;i++) { Account uacc= new Account(); uacc.Name='TestUpdateAccounts ultimateAccount'+i; uacc.BillingStreet='TestUpdateAccounts'+i+'Street'; uacc.Client_Short_Name__c ='CTypeUp'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.BillingCountry='US'; uacc.phone='111111'; uacc.BillingCity='TestUpdateAccounts'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Strategic Account'; uacc.Sub_Type__c = 'Corporate'; ultimateAccountList.add(uacc); } insert ultimateAccountList; // Insert child Accounts whose UParent is Uparent1 for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChanged Child'+i; acc.BillingStreet='UPTypeAndUPClientIsChanged Child Street' + i; acc.Client_Short_Name__c ='test1'; acc.BillingState='WI'; acc.BillingPostalCode='5322'+i; acc.BillingCountry='US'; acc.phone='111111'; acc.BillingCity='UPTypeAndUPClientIsChanged City'+i; acc.Ultimate_Parent_Client__c = ultimateAccountList[0].Id; childAcclist.add(acc); } test.startTest(); insert childAcclist; List<Account> updateClients = new List<Account>(); //Modify the SubType of UParent1 to "International" and add them to updateClients List. Account updateUPAccount = [SELECT TYPE FROM Account WHERE ID =: ultimateAccountList[0].Id ORDER BY ID]; updateUPAccount.Sub_Type__c = 'International'; updateClients.add(updateUPAccount); //Modify the Ultimate_Parent_Client of the first 20 child clients to UParent2 //and add them to updateClients List. for(Account a : [SELECT ID,Sub_Type__c,Ultimate_Parent_Client__c FROM Account WHERE ID =: childAcclist ORDER BY ID LIMIT 20] ) { a.Ultimate_Parent_Client__c = ultimateAccountList[1].Id; updateClients.add(a); } //Update the Accounts List update updateClients; //Retrieve all the child accounts and verify the Sub-Type // List<Account> accList = [SELECT Sub_Type__c FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=0;i<20;i++) System.assertEquals(updateClients[i].Sub_Type__c,'Corporate'); for(Integer i=20;i<40;i++) System.assertEquals(updateClients[i].Sub_Type__c,'International'); test.stopTest(); } /* Input: No input variables Process: 1. Insert an Ultimate Parent Account whose Type is 'Strategic Account' and Sub Type is "Corporate" 2.Insert 40 child Accounts whose Ultimate Parent Account equals the above UP, Type is 'Global Account' and SubType = 'International' Output: Validate the SubType field on child Clients. The Sub-TYPE of the child accounts should be 'Corporate'. */ static testMethod void childSubTypeNotEqualsUParentSubTypeOnInsert() { List<Account> childAcclist = new List<Account>(); // Insert an Ultimate Parent Accounts whose Type is 'Strategic' and Sub-Type is 'Corporate' Account uacc= new Account(); uacc.Name='childTypeNEUParentTypeOnInsert'; uacc.BillingStreet='childTypeNEUParentTypeOnInsertStreet'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.BillingCountry='US'; uacc.phone='111111'; uacc.BillingCity='childTypeNEUParentTypeOnInsertCity'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Strategic Account'; uacc.Sub_Type__c = 'Corporate'; insert uacc; test.startTest(); // Insert child Accounts whose UParent is uacc for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChangedChild'+i; acc.Client_Short_Name__c =('UPTypeAndUPClientIsChangedChild'+i).substring(0,8); acc.BillingStreet='UPTypeAndUPClientIsChangedChildStreet' + i; acc.BillingState='WI'; acc.BillingPostalCode='5322'+i; acc.phone='111111'; acc.BillingCountry='US'; acc.BillingCity='UPTypeAndUPClientIsChangedCity'+i; acc.Ultimate_Parent_Client__c = uacc.Id; acc.Type = 'Global Account'; acc.Sub_Type__c = 'International'; childAcclist.add(acc); } insert childAcclist; // List<Account> accList = [SELECT TYPE,Sub_Type__c FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=20;i<40;i++) System.assertEquals(childAcclist[i].Sub_Type__c,'Corporate'); test.stopTest(); } /* Input: No input variables Process: 1. Insert an Ultimate Parent Account whose Type is 'Other Key Account' 2.Insert 40 child Accounts whose Ultimate Parent Account equals the above UP 3. Update the Type ='Strategic Account'and Sub-Type = 'National' on the child accounts Output: Validate the Type field on Clients. The Sub-TYPE of the child accounts should be null. Changes: */ static testMethod void childSubTypeNotEqualsUParentSubTypeOnUpdate() { List<Account> childAcclist = new List<Account>(); List<Account> ultimateAccountList = new List<Account>(); // Insert an Ultimate Parent Accounts whose Type is 'Other Key Account' Account uacc= new Account(); uacc.Name='childTypeNEUParentTypeOnUpdate'; uacc.BillingStreet='childTypeNEUParentTypeOnUpdateStreet'; uacc.BillingState='CA'; uacc.BillingPostalCode='12345'; uacc.phone='111111'; uacc.BillingCountry='US'; uacc.BillingCity='childTypeNEUParentTypeOnUpdateCity'; uacc.Ultimate_Parent__c = true; uacc.Type = 'Strategic Account'; uacc.Sub_Type__c = 'Corporate'; insert uacc; test.startTest(); // Insert child Accounts whose UParent is uacc.ID for(Integer i=0;i<40;i++) { Account acc = new Account(); acc.Name='UPTypeAndUPClientIsChanged Child'+i; acc.BillingStreet='UPTypeAndUPClientIsChangedChildStreet' + i; acc.BillingState='WI'; acc.BillingPostalCode='5322'+i; acc.phone='111111'; acc.BillingCountry='US'; acc.BillingCity='UPTypeAndUPClientIsChanged City'+i; acc.Ultimate_Parent_Client__c =uacc.Id; childAcclist.add(acc); } insert childAcclist; List<Account> updateClients = new List<Account>(); //Update the Type from Other Key Account to Strategic Account and Sub-Type to 'National'' for(Account a : [SELECT ID,TYPE,Ultimate_Parent_Client__c,Sub_Type__c FROM Account WHERE ID =: childAcclist ORDER BY ID LIMIT 20] ) { a.Sub_Type__c = 'National'; updateClients.add(a); } update updateClients; //Retrieve all the child accounts and verify the Type //List<Account> accList = [SELECT TYPE,Sub_Type__c FROM Account WHERE ID in : childAcclist order by id]; for(Integer i=0;i<40;i++) System.assertEquals('Corporate',updateClients[i].Sub_Type__c); test.stopTest(); } }
[1] http://blog.deadlypenguin.com/blog/testing/strategies/