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
soma s 6soma s 6 

test trigger

Could you please advise me how to write test class for below


trigger Dcalup on Account (after update) {

  for (Account myacc : Trigger.old) {
    if (myacc.StrPhr_Do_Not_Call_Group_Practice__c == True) {
      List<Account> Dccall = [SELECT StrPhr_Do_Not_Call_Group_Practice__c from Account where Phone= :myacc.Phone];                
      if (Dccall.size() > 0)        
       for (Account a : Dccall ) {
         a.StrPhr_Do_Not_Call_Group_Practice__c = True;
        update a;
}      
             }                            
    }
  }
Best Answer chosen by soma s 6
Mahesh DMahesh D
Hi Soma,

Please find the below modified code:

Here I considered:

(1) Corrected the logic.
(2) Bulkified the Trigger.
(3) Naming Convention.
(4) Alignment.
(5) Tested the code in my DE environment and it shows 100% with assertions.

 
trigger AccountTrigger on Account (after update) {
    Set<String> phoneStrSet = new Set<String>();
    
    for(Account acc: Trigger.new) {
        if (acc.StrPhr_Do_Not_Call_Group_Practice__c == True && 
                    acc.StrPhr_Do_Not_Call_Group_Practice__c != Trigger.oldMap.get(acc.Id).StrPhr_Do_Not_Call_Group_Practice__c) {
            phoneStrSet.add(acc.Phone);
        }    
    }
    
    if(!phoneStrSet.isEmpty()) {
        List<Account> accList = [Select Id, StrPhr_Do_Not_Call_Group_Practice__c from Account where Phone IN: phoneStrSet AND ID NOT IN: Trigger.newMap.keySet()];
        if(accList != null && !accList.isEmpty()) {
            for(Account acc: accList) {
                acc.StrPhr_Do_Not_Call_Group_Practice__c = True;
            }
            update accList;
        }
    }
}

Test Class:
 
@isTest
private class AccountTrigger3Test {
    @isTest
    static void testMethodOne() {
        Account accOne = new Account();
        accOne.Name='Test Account One';
        accOne.StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accOne;
        
        Account accTwo = new Account();
        accTwo.Name='Test Account Two';
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accTwo;
        
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = true;
        update accTwo;
               
        Account acc = [select Id, StrPhr_Do_Not_Call_Group_Practice__c from Account where Id =: accOne.Id LIMIT 1];
        
        System.assertEquals(true, acc.StrPhr_Do_Not_Call_Group_Practice__c);
    }
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Soma,

Please find the below modified code:

Here I considered:

(1) Corrected the logic.
(2) Bulkified the Trigger.
(3) Naming Convention.
(4) Alignment.
(5) Tested the code in my DE environment and it shows 100% with assertions.

 
trigger AccountTrigger on Account (after update) {
    Set<String> phoneStrSet = new Set<String>();
    
    for(Account acc: Trigger.new) {
        if (acc.StrPhr_Do_Not_Call_Group_Practice__c == True && 
                    acc.StrPhr_Do_Not_Call_Group_Practice__c != Trigger.oldMap.get(acc.Id).StrPhr_Do_Not_Call_Group_Practice__c) {
            phoneStrSet.add(acc.Phone);
        }    
    }
    
    if(!phoneStrSet.isEmpty()) {
        List<Account> accList = [Select Id, StrPhr_Do_Not_Call_Group_Practice__c from Account where Phone IN: phoneStrSet AND ID NOT IN: Trigger.newMap.keySet()];
        if(accList != null && !accList.isEmpty()) {
            for(Account acc: accList) {
                acc.StrPhr_Do_Not_Call_Group_Practice__c = True;
            }
            update accList;
        }
    }
}

Test Class:
 
@isTest
private class AccountTrigger3Test {
    @isTest
    static void testMethodOne() {
        Account accOne = new Account();
        accOne.Name='Test Account One';
        accOne.StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accOne;
        
        Account accTwo = new Account();
        accTwo.Name='Test Account Two';
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accTwo;
        
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = true;
        update accTwo;
               
        Account acc = [select Id, StrPhr_Do_Not_Call_Group_Practice__c from Account where Id =: accOne.Id LIMIT 1];
        
        System.assertEquals(true, acc.StrPhr_Do_Not_Call_Group_Practice__c);
    }
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
soma s 6soma s 6
Wow wow wow Magesh , what a dedication and sincere reply. Million thanks Mahesh.
regards
soma