You need to sign in to do that
Don't have an account?
Victoria 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){
}
}
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){
}
}
use this code
All Answers
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"
use this code
Raj,
The code coverage was only 65%, do you know how to improve the code coverage? I apologize for asking so many questions
@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');
}
}