You need to sign in to do that
Don't have an account?
Jennifer 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}
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}
Your insert line should look like the below:
insert a;
All Answers
Try below, Best Regards
Sampath
Your insert line should look like the below:
insert a;