I need to write test class for an account traversing trigger. What trigger does is say for my account record,it will fetch the parent account in top level.

Issue now is how do i pass parent id to my test class.This is what i have been trying so far and it has been failing

private class TEST_GEN_FindGlobalUltimate 

public static testMethod void createAccount() 
    list<account>parentlist = new list<account>();  
    list<account>childlist = new list<account>();   
    for (integer i = 0;i<2;i++)
            parentlist.add(new account(name='Parent'+i));           
        for(integer i =0;i<2;i++);

        for( integer i = 0; i<2;i++)
            system.debug('Parent is'+ parentlist[i].id);
            childlist.add(new account(name = 'acc1'+i,parentid = parentlist[i].id));

Code that this is calling is listed below https://codereview.stackexchange.com/questions/29245/account-hierarchy-code-review

i think i know why its not working when i try to insert,since there is a static variable check on trigger to make sure it is not called twice.I need to update it by using isRunningTest. Is that the correct way to handle this?

Modifying my trigger class code saying

{ firstRun_GlobalUltimate = true; } 

Is that a good practice?

  • Do you get an error message? Aug 12, 2013 at 8:45
  • Yes. Link out of index.and when i comment statements involving parentlist[i].id in second method,error disapperas
    – sfdc99999
    Aug 12, 2013 at 8:49
  • Can you show us the code in which you call these functions? Aug 12, 2013 at 8:52
  • yeah updated in query
    – sfdc99999
    Aug 12, 2013 at 9:09
  • 1
    i think i know why its not working when i try to insert,since there is a static variable check on trigger to make sure it is not called twice.I need to update it by using isRunningTest
    – sfdc99999
    Aug 12, 2013 at 9:23

1 Answer 1


Creating your test class is an entirely different issue than creating your trigger. Create the accounts your want to test as children of Parent Accounts. That will solve your problem for the Test Class. It then becomes a matter of keeping track of which list of Accounts you're you're referring to during your testing.

In essence, the process goes something like this:

// Create Parent Accounts: 

   Account p = new Account(Name = 'pacct' + string.value of(i), street = 101+i p street, etc ); // assumes you need to populate the added fields
if(PAccts.isEmpty() == false) {
   Database.SaveResult[] PAcctsInsrtRslts = Database.Insert(PAccts,true);
     for(Database.SaveResult PA: PAcctsInsrtRslts){

// You now have the ID's for the parent accounts to use for creating the child accounts
// They're the ID's contained in list<Id>maIds

// Create child accounts

   Account c = new Account(Name = 'cacct' + string.value of(i), parent=paIds[i], street = 101+i p street, etc ); // again, assumes you need to populate the added fields
if(CAccts.isEmpty() == false) {
   Database.SaveResult[] CAcctsInsrtRslts = Database.Insert(CAccts,true);
     for(Database.SaveResult CA: CAcctsInsrtRslts){
// you now have the ID's for the child accounts to use for your test class

I hope this provides you with what you need. In essence, you won't be able to create them separately using any kind of "call sub class" method code unless you can pass the Account Id's of a list to the class you might be calling when you create the child account records. Sorry, but I didn't have time to refer to the link you posted for the exact details. I just know that this method will create the type of records you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.