You need to sign in to do that
Don't have an account?
uHaveOptions
Account Primary Contact Test Class Help
I'm 68% to pass my trigger and close to getting 75% . i just need a few more lines to pass. can anyone guide me for this to pass?
Here is the trigger
Trigger PrimaryContact on Contact (Before Insert, Before Update, After Insert, After Update, After Delete, After UnDelete) { List<Id> actIds = New List<Id>(); List<Contact> comingCons = New List<Contact>(); List<Id> conIds = New List<Id>(); If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) { For(Contact Con : Trigger.New) { If(Con.Primary_Contact__c == TRUE) { actIds.add(Con.AccountId); conIds.add(Con.Id); comingCons.add(Con); } } } List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds) FROM Account WHERE Id =: actIds]; For(Contact EveryCon : comingCons) { For(Account EveryAccount : allRelatedAccounts){ If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){ EveryCon.addError('There is already a primary contact for this account'); } } } List<Id> accountIds = New List<Id>(); If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){ For(Contact Con : Trigger.New){ If(Con.AccountId != NULL){ accountIds.add(Con.AccountId); } } } If(Trigger.IsDelete){ For(Contact Con : Trigger.Old){ If(Con.AccountId != NULL){ accountIds.add(Con.AccountId); } } } List<Account> actFinalListToUpdte = New List<Account>(); For(Account act : [Select ID, Contact2__c, (Select Id, FirstName, LastName, Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1) FROM Account WHERE Id =: accountIds]) { If(act.Contacts.size() > 0) { act.Contact2__c = act.Contacts[0].Id; actFinalListToUpdte.add(act); } } try{ If(!actFinalListToUpdte.isEmpty()){ update actFinalListToUpdte; } }Catch(Exception e){ system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage()); } }
This are the line not passing
For(Contact EveryCon : comingCons) { For(Account EveryAccount : allRelatedAccounts){ If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){ EveryCon.addError('There is already a primary contact for this account'); } } } If(Trigger.IsDelete){ For(Contact Con : Trigger.Old){ If(Con.AccountId != NULL){ accountIds.add(Con.AccountId); } } } For(Account act : [Select ID, Contact2__c, (Select Id, FirstName, LastName, Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1) FROM Account WHERE Id =: accountIds]) { If(act.Contacts.size() > 0) { act.Contact2__c = act.Contacts[0].Id; actFinalListToUpdte.add(act); } }
This is the test class and its 68%
@isTest public class PrimaryContactTest { static testMethod void createAccount(){ Account a = new Account(); a.Name = 'test Co.'; insert a; System.debug('created account'); Contact c = new Contact(); c.FirstName = 'John'; c.LastName = 'Doe'; c.Primary_Contact__c = True; Insert c; System.debug('Insert primary contact'); Contact ca = new Contact(); ca.FirstName = 'John'; ca.LastName = 'Doe'; ca.Primary_Contact__c = False; Insert ca; System.debug('Insert primary contact'); Update a; } }
Let us know if this will help you
All Answers
Let us know if this will help you
Let us know if this will help you