3

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

@isTest
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++);
        {

        }
        insert(parentlist);         
        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));
        }
        insert(childlist);  
}   
}

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

 if(test.isrunningtest()) 
{ firstRun_GlobalUltimate = true; } 

Is that a good practice?

  • Do you get an error message? – rael_kid Aug 12 '13 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 '13 at 8:49
  • Can you show us the code in which you call these functions? – rael_kid Aug 12 '13 at 8:52
  • yeah updated in query – sfdc99999 Aug 12 '13 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 '13 at 9:23
2

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: 

for(i=0;i<listsize<i++){
   Account p = new Account(Name = 'pacct' + string.value of(i), street = 101+i p street, etc ); // assumes you need to populate the added fields
   PAccts.add(m);
}
if(PAccts.isEmpty() == false) {
   Database.SaveResult[] PAcctsInsrtRslts = Database.Insert(PAccts,true);
     for(Database.SaveResult PA: PAcctsInsrtRslts){
        if(PA.IsSuccess()){
           paIds.add(PA.getId());
        }
     }
     system.assertEquals(paIds.size(),listsize);
}   

// 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

for(i=0;i<listsize<i++){
   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
   CAccts.add(m);
}
if(CAccts.isEmpty() == false) {
   Database.SaveResult[] CAcctsInsrtRslts = Database.Insert(CAccts,true);
     for(Database.SaveResult CA: CAcctsInsrtRslts){
        if(CA.IsSuccess()){
           caIds.add(CA.getId());
        }
     }
     system.assertEquals(caIds.size(),listsize);
}   
// 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.

| improve this answer | |

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.