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
Victoria BovaVictoria Bova 

What would be the test class code for this contact trigger? I am having trouble getting trigger coverage

I am very new to Salesforce and the world of coding so please be very descriptive with your answers. Here is the code: 

trigger ContactSumTriggers 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){
       
   }
}
Best Answer chosen by Victoria Bova
Raj VakatiRaj Vakati
Its because of duplicate rule 

use this code
 
@isTest
public class ConTrigger_Test {
static testmethod void testCon(){

Account acc = new Account(Name='Accoun1',phone='394659634',website='testttttttt.com');
		insert acc;
	

Account acc1 = new Account(Name='Not abel to find',phone='560897098678',website='asdasd.com');
		insert acc1;	
		
Contact con = new Contact() ;
con.LastName='Test';
con.FirstName='Test';
con.Email='asdasd@sdfsd.com';
con.AccountId =acc.Id ;
insert con ;		
 
 con.AccountId =acc1.Id ;
 update con ; 
 
 delete con;
}
}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
@isTest
public class ConTrigger_Test {
static testmethod void testCon(){

Account acc = new Account(Name='Dummy Account',phone='123123123',website='asdasd.com');
		insert acc;
	

Account acc1 = new Account(Name='Dummy Account',phone='123123123',website='asdasd.com');
		insert acc1;	
		
Contact con = new Contact() ;
con.LastName='Test';
con.FirstName='Test';
con.Email='asdasd@sdfsd.com';
con.AccountId =acc.Id ;
insert con ;		
 
 con.AccountId =acc1.Id ;
 update con ; 
 
 delete con;
}
}

 
Victoria BovaVictoria Bova
Hi Raj

Thanks for the quick response! Unfortunately, this code failed when I ran a test and I received this error message: "System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You are creating a duplicate record. We recommend you use an existing record instead"
Raj VakatiRaj Vakati
Its because of duplicate rule 

use this code
 
@isTest
public class ConTrigger_Test {
static testmethod void testCon(){

Account acc = new Account(Name='Accoun1',phone='394659634',website='testttttttt.com');
		insert acc;
	

Account acc1 = new Account(Name='Not abel to find',phone='560897098678',website='asdasd.com');
		insert acc1;	
		
Contact con = new Contact() ;
con.LastName='Test';
con.FirstName='Test';
con.Email='asdasd@sdfsd.com';
con.AccountId =acc.Id ;
insert con ;		
 
 con.AccountId =acc1.Id ;
 update con ; 
 
 delete con;
}
}

 
This was selected as the best answer
Victoria BovaVictoria Bova
That makes sense. The code passed, thanks again!
 
Victoria BovaVictoria Bova
User-added image
Raj,

The code coverage was only 65%, do you know how to improve the code coverage? I apologize for asking so many questions
Raj VakatiRaj Vakati
Let me write again 
Raj VakatiRaj Vakati
This trigger is getting 96 % and there is a class "TestLightningConnectQuickStart" whihc is failing due to validation rule .. Please check that class 
Victoria BovaVictoria Bova
I am confused because I thought the code you wrote was supposed to be the class. I will look into that class now though and get back to you! Thank you for your patience 
Victoria BovaVictoria Bova
This is the code for that class. Is there any way I can unlink it to the trigger without deleting the class?


@isTest
public class TestLightningConnectQuickstart {
    public static testMethod void tester() {
        List<Account> accs = new List<Account>();
        for (Integer i = 0; i < 10; i++) {
            accs.add(new Account(name = 'Test '+i));
        }
        insert accs;
        
        LightningConnectQuickstart qs = new LightningConnectQuickstart();
        
        Boolean state = qs.getCustomerIDsSet();
        
        System.assertEquals(false, state, 'IDs should not be set');
        
        qs.setCustomerIDs();

        state = qs.getCustomerIDsSet();
        
        System.assertEquals(true, state, 'IDs should be set');

    // Double check!
    accs = [SELECT Id 
                FROM Account 
                WHERE Customer_Id__c = null];
        
        System.assertEquals(0, accs.size(), 'IDs should be set');
    }
}
Raj VakatiRaj Vakati
I dnt think so .. you need to comment that code 
Victoria BovaVictoria Bova
I got the ConTrigger_Test code to deploy into production! Thank you! Now my issue is getting to deploy my original trigger because it has 0% coverage. Would you happen to know how to solve that problem as well?
 
Victoria BovaVictoria Bova
I got it all to work! A final thank you, Raj for being a tremendous help!