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
SandeepbvSandeepbv 

Code Coverage for Trigger- Need help

Hello,

I created a before insert trigger which uses sosl queries to search for field value 'phone number' of call_log__c object in salesforce.
  • If phone is matched with Account phone number, it links call_log__c object with Account.
  • If phone is matched with phone_book__c phone, it links call_log__c object with phone_book__c.
  • If no record is found, then new phone_book__c record is created, and is linked with call_log__c object.
I have created a test class for trigger, but I could only test about 64%(11 of 17 Lines) of the trigger code. I have tried everything I can think of. Please, suggest me what I need to do in order to get 100% coverage.

Here's my trigger and test class
 
trigger MatchPhoneNumber on Call_Log__c (before insert) {
    
	List<List<sObject>> searchList = [FIND :Trigger.New[0].Caller_Id__c IN ALL FIELDS 
                   RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
    
	Account[] searchAccounts = (Account[])searchList[0];
	Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
    
    if(!searchAccounts.isEmpty()){
		for (Account a : searchAccounts) {
        	for(Call_Log__c c : Trigger.New) {
        		c.member__c = a.ID;
    		}
    	}
    }
    
    if(!searchPhones.isEmpty()){
    	for (Phone_Book__c p : searchPhones) {
            for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    	}
    }
    else{
        Phone_Book__c p = new Phone_Book__c();
        p.name = Trigger.New[0].Caller_Id__c;
        p.caller_id__c = Trigger.New[0].Caller_Id__c;
        insert p;
        for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    }

}
 
@isTest 
public class TestMatchPhoneNumber {
    static testMethod void insertcalllog(){
        Account acc = new Account();
        acc.LastName = 'Jas';
        acc.Phone = '1234567890';
		acc.Security_Key__c = '1as4s22';
        insert acc;
        System.assertEquals(1,[select count() from Account  where LastName = 'Jas' ]);
        
        Phone_Book__c p = New Phone_book__c();
        p.name = '1234567890';
        insert p;
        System.assertEquals(1,[select count() from Phone_Book__c  where Name = '1234567890' ]);
        
        Call_Log__c c1 = New Call_Log__c();
        	Call_Log__c c2 = New Call_Log__c();

            c1.Caller_ID__c = '1234567890';
            c2.Caller_ID__c = '1111111111';
            insert c1;
            insert c2;
        Test.startTest();
            Id [] fixedSearchResults= new Id[2];
            fixedSearchResults[0] = acc.Id;
            fixedSearchResults[1] = p.Id;
            Test.setFixedSearchResults(fixedSearchResults);
            List<List<sObject>> searchList = [FIND '1234567890' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
        	List<List<sObject>> searchList2 = [FIND '1111111111' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
            Account[] searchAccounts = (Account[])searchList[0];
			Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
        
            Account[] searchAccounts2 = (Account[])searchList2[0];
			Phone_Book__c[] searchPhones2 =(Phone_Book__c[])searchList2[1];
        
            
        Test.stopTest();
        
        System.assert(!searchAccounts.isEmpty());
        System.assert(!searchPhones.isEmpty());  
        System.assert(!searchAccounts2.isEmpty());
        System.assert(!searchPhones2.isEmpty()); 
        
        
            
            c1.member__c = acc.ID;
      	
        
         
                c1.Phone_Number__c = p.ID;     
    	
    
        Phone_Book__c ph = New Phone_book__c();
        ph.Name = c2.Caller_ID__c;
        ph.Caller_ID__c = c2.Caller_ID__c;
        insert ph;
        Call_log__c call = [Select id, member__c, phone_number__c from call_log__c where caller_id__c = '1234567890'];
        List<Phone_book__c> pl = [Select id from Phone_book__c where id = :ph.Id];
        System.assertEquals( pl.size() , 1 );
		//System.assertEquals(fixedSearchResults[0], call.member__c);  
		//System.assertEquals(fixedSearchResults[1], call.phone_number__c);  
     
    }
}

Thanks in advance
Jas
Paul S.Paul S.
Try including tests for situations where searchAccounts and searchPhone are empty.  You need to test both postitive and negative use cases for the code in an if statement to be covered.
SandeepbvSandeepbv
Paul, in my code coverage, it is showing for loops are not covered. I have looked online for resources, how to create test cases for nested for ​loops. Attaching a screenshot

.User-added image

Please, help me how can test this part of code.

Thanks,
Jas
Paul S.Paul S.
Create a second test method, but in this one change the phone number of the account you're inserting to something that does NOT match the caller Id of one of the call logs and then change your assertions to assert that the searchAccounts and searchAccounts2 lists are empty.  This will leave you with a test class that tests both sides of the if statement on line 9.  In one case you'll have an empty searchAccounts list and in the other you'll have data there.

Also, your test.startTest and test.stopTest should be around the insertion of the call log records (insert c1 and insert c2).