0

I have a requirement, where i need to populate all the related contacts in Account Object Detail Page with comma seperated. For Example : If I have Account1 and Contact1 and Contact2 are related to Account1. Then, in account object detail page, i have a text box named "Related Contacts" which will display Contact1, Contact2.

Below is the trigger for that --

 trigger totalContacts on Contact (after insert,after update) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }

    List<Account> accounts = [Select ID, Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String contactName = '';
        for(Contact c : a.Contacts){
            contactName +=c.Name+ ' ,';                      
        }
        a.Associated_Contacts__c =contactName;
    }
    update accounts;

}

I wrote the below test class and it covers only 60%, please guide me to cover 100% of the code

@isTest 
public class testtotalContacts{
    static testMethod void insertnewContact(){

    Account accountToCreate = new Account();
    accountToCreate.Name = 'Test Account';
    accountToCreate.AW_Expiration_date__c = System.today();
    insert accountToCreate;

    Contact contactToCreate1 = new Contact();
    contactToCreate1.FirstName = 'Test 1';
    contactToCreate1.LastName = 'Trigger test class 1';
    contactToCreate1.Email = '[email protected]';
    contactToCreate1.Account = accountToCreate;
    insert contactToCreate1;

    Contact contactToCreate2 = new Contact();
    contactToCreate2.FirstName = 'Test 2';
    contactToCreate2.LastName = 'Trigger test class 2';
    contactToCreate2.Email = '[email protected] 2';
    contactToCreate2.Account = accountToCreate;
    insert contactToCreate2;


    }
}
4
  • can you share which lines are not getting covered. Commented Dec 3, 2015 at 5:11
  • Once you get the lines covered how are you going to know that the trigger is actually working? You won't given your current test class
    – Eric
    Commented Dec 3, 2015 at 5:27
  • I think that the issue might be outside of this test class. By looking at the code, every single run of the trigger will run all the lines in it, so it should be fully covered. Perhaps you have other classes in your org that are not covered by tests, and perhaps that's where you need to look?
    – mkorman
    Commented Dec 3, 2015 at 14:47
  • (1) it is dubious that the trigger will ever run after insert as without an Account, a Contact can't be located until the Account exists. (2) As an complete alternative - you could insert a VF page into the standard layout that displayed a scrollable list (not comma-separated) of Contacts that could be clicked to edit. No Apex code at all
    – cropredy
    Commented Dec 3, 2015 at 17:09

1 Answer 1

0

After looking at your code, it should throw following error

SObject row was retrieved via SOQL without querying the requested field

You are not querying "Associated_Contacts__c" field in account query, but later updating it in the code. Update your query as below:

List<Account> accounts = [Select ID, Associated_Contacts__c Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];

Update your test class as

@isTest 
public class testtotalContacts{
static testMethod void insertnewContact(){

Account accountToCreate = new Account();
accountToCreate.Name = 'Test Account';
insert accountToCreate;

Contact contactToCreate1 = new Contact();
contactToCreate1.FirstName = 'Test 1';
contactToCreate1.LastName = 'Trigger test class 1';
contactToCreate1.Email = '[email protected]';
contactToCreate1.AccountID = accountToCreate.id;
insert contactToCreate1;

Contact contactToCreate2 = new Contact();
contactToCreate2.FirstName = 'Test 2';
contactToCreate2.LastName = 'Trigger test class 2';
contactToCreate2.Email = '[email protected]';
contactToCreate2.AccountID = accountToCreate.id;
insert contactToCreate2;


}
}
5
  • Hi all, Thanks for your comments. Below code is not getting covered String contactName = ''; for(Contact c : a.Contacts){ contactName +=c.Name+ ' ,'; } a.Associated_Contacts__c =contactName; ..................... Trigger code is working fine !!
    – user26810
    Commented Dec 3, 2015 at 6:16
  • Have you updated your query as I recommended? Commented Dec 3, 2015 at 6:20
  • Hi Salesforcesmarty, Yes. I have updated the code as sugessted by you. kindly check.
    – user26810
    Commented Dec 4, 2015 at 5:09
  • @user26810 Find the updated test class. Commented Dec 4, 2015 at 5:27
  • Hi Salesforcesmarty, Your class worked perfect and I got 100% code coverage....Thanks !!
    – user26810
    Commented Dec 7, 2015 at 6:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .