Skip to main content
John Collins (Harver) 님이 #Apex에 질문했습니다
Hi, I am an apex rookie and need some help writing a test class for the following code. Can anyone help me out?

trigger ContactSumTrigger on Contact (After insert, After delete, After undelete, After Update) {

    Set<Id> parentIdsSet = new Set<Id>();

    List<Account> accountListToUpdate = new List<Account>();

    IF(Trigger.IsAfter){

        IF(Trigger.IsInsert || Trigger.IsUndelete  || Trigger.isUpdate){

            FOR(Contact c : Trigger.new){

                if(c.AccountId!=null){   

                   parentIdsSet.add(c.AccountId);

                   If(Trigger.isUpdate){

                       if(Trigger.oldMap.get(c.Id).AccountId != c.AccountId){

                          parentIdsSet.add(Trigger.oldMap.get(c.Id).AccountId);

                       }

                    }

                }

            }

        }

        IF(Trigger.IsDelete){

            FOR(Contact c : Trigger.Old){

                if(c.AccountId!=null){   

                   parentIdsSet.add(c.AccountId); 

                }

            }

        }

    }

    System.debug('⌗⌗⌗⌗ parentIdsSet = '+parentIdsSet);

    List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]);

    FOR(Account acc : accountList){

        List<Contact> contactList = acc.Contacts;

        acc.Number_of_Contacts__c = contactList.size();

        accountListToUpdate.add(acc);

    }

    try{

        update accountListToUpdate;

    }catch(System.Exception e){

        

    }

}
답변 3개
  1. 2018년 5월 14일 오후 7:58
    Hi John,

    In your apex trigger,

    Replace:

     try{

            update accountListToUpdate;

        }catch(System.Exception e){

         } 

    With:

    if(accountListToUpdate.size()>0){

            update accountListToUpdate;

    }

    And then your test class would be like:

    @istest

    public class ContactSumTriggerTest {

    @testsetup

    static void setup(){

    Account a1=new account();

    a1.Name='Test Account 1';

    insert a1;

    Account a2=new account();

    a2.Name='Test Account 2';

    insert a2;

    Contact c=new contact();

    c.AccountId=a1.Id;

    c.LastName='Test Contact';

    insert c;

    }

    public static testmethod void Updatefn(){

    Account a=[select id,name from account where name='Test Account 2'];

    contact c=[select id, name, accountID from contact where lastname='Test Contact' limit 1];

    c.LastName='Test Contact Updated';

    c.AccountId=a.Id;

    test.startTest();

    try{

    update c;

    }catch(System.Exception e){

    system.debug(e.getMessage());

    }

    test.stopTest();

    }

    public static testmethod void Deletefn(){

    contact c=[select id, name, accountID from contact where lastname='Test Contact' limit 1];

    test.startTest();

    try{

    delete c;

    }catch(System.Exception e){

    system.debug(e.getMessage());

    }

    test.stopTest();

    }

    }

    Let me know if it helps.

    Thanks!
0/9000