function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jennifer LaingJennifer Laing 

Apex Test Class for Trigger to update Child Account Owners to Parent Account Owners when Child created

Hello

I'm hoping someone can help me write a Test Class for this Trigger please?  I have attempted it (first time!) but am having issues with the "insert" line/element (note, the ID's are false ID's for this example):

Test Class:

@isTest 
public class ChildAccountOwnerUpdate {
        static testMethod void ParentChildOwner()   {
        
        Account a = new Account();
        a.Name ='Test Account' ;
        a.ParentId = '0018E00000glxyz';
        a.OwnerId = '005D0000007dxyz';    
        insert ;
       
            }

}

Trigger:

Trigger ParentChildOwner on Account (Before Insert, Before Update, After UnDelete) {
02     
03    List<Account> comingAccounts      = New List<Account>();
04    List<Id> comingAccountsParentIds  = New List<Id>();
05     
06    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
07        For(Account act : Trigger.New)
08        {
09            If(act.ParentId != NULL)
10            {
11               comingAccounts.add(act);
12               comingAccountsParentIds.add(act.ParentId);
13            }
14             
15        }
16    }
17     
18    List<Account> fetchingParentAccount = [Select Id, OwnerID FROM Account WHERE Id=:comingAccountsParentIds AND OwnerID != NULL];
19    //system.debug('The size of fetchingParentAccount is: ' + fetchingParentAccount.size());
20     
21    For(Account EveryAct : comingAccounts)
22    {
23        //system.debug('EveryAct existing owner id is: ' + EveryAct.OwnerId);
24         
25        For(Account EveryParent : fetchingParentAccount)
26        {
27            If(EveryAct.ParentId == EveryParent.Id)
28            {
29                EveryAct.OwnerId = EveryParent.OwnerId;
30            }
31        }
32         
33        //system.debug('EveryAct new owner id is: ' + EveryAct.OwnerId);
34    }
35}
Best Answer chosen by Jennifer Laing
charforcecharforce
You need to create the parent Account in your test class first. User is one of the objects you don't need to create test data for, refer here for the complete list https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm

Your insert line should look like the below:
insert a;
 
@isTest
private class ChildAccountOwnerUpdate {
    
    static testMethod void parentChildOwner()   {     
        // insert parent Account
        Account parentAccount = new Account();
        parentAccount.Name ='Parent Account';
        parentAccount.OwnerId = '005D0000007dxyz';    
        insert parentAccount;
        
        // insert child Account
        Account childAccount = new Account();
        childAccount.Name ='Test Account';
        childAccount.ParentId = parentAccount.Id;
        childAccount.OwnerId = '005D0000007dxyz';    
        insert childAccount;
        
        System.assertEquals(parentAccount.OwnerId, childAccount.OwnerId);       
    }
    
}

 

All Answers

Sampath SuranjiSampath Suranji
Hi Jennifer Laing,
Try below,
@isTest
public class ChildAccountOwnerUpdate {
    
    static testMethod void ParentChildOwner()   {
        
        Account objAcc = new Account();
        objAcc.Name ='Test Account' ;
        Account objParentAcc = new Account(Name='test parent');
        insert objParentAcc;
        Profile objProfile = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User objUser = new User(Alias = 'stdtusr', Email='standarduser@testclass.com', 
                                EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = objProfile.Id, 
                                TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testclass.com');
        
        insert objUser;         
        objAcc.ParentId = objParentAcc.Id;
        objAcc.OwnerId = objUser.Id;    
        insert objAcc ;
        
    }
    
}
Best Regards
Sampath
 
charforcecharforce
You need to create the parent Account in your test class first. User is one of the objects you don't need to create test data for, refer here for the complete list https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm

Your insert line should look like the below:
insert a;
 
@isTest
private class ChildAccountOwnerUpdate {
    
    static testMethod void parentChildOwner()   {     
        // insert parent Account
        Account parentAccount = new Account();
        parentAccount.Name ='Parent Account';
        parentAccount.OwnerId = '005D0000007dxyz';    
        insert parentAccount;
        
        // insert child Account
        Account childAccount = new Account();
        childAccount.Name ='Test Account';
        childAccount.ParentId = parentAccount.Id;
        childAccount.OwnerId = '005D0000007dxyz';    
        insert childAccount;
        
        System.assertEquals(parentAccount.OwnerId, childAccount.OwnerId);       
    }
    
}

 
This was selected as the best answer
Jennifer LaingJennifer Laing
Thank you so much for your help, its appreciated.