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
Dean BlanchDean Blanch 

I have created an apex trigger in sandbox to add the field "Number of Contacts" in to the account! but need to run a test...

I need to create a test for it can anyone help provide the coding for the test please? (possibly with directions on how to get the process done)?


trigger  NumberOfContacts on Contact (after insert) {
set<id> setid = new set<id>();

list<account> listaccount = new list<account>();
for(contact con : trigger.new){

setid.add(con.accountid);
}

For(account acc : [select id,name,Number_of_Contacts__c, (select id from contacts)from account where id=:setid]) {
Account ac1 = new account();

ac1.id=acc.id;

ac1.Number_of_Contacts__c= acc.contacts.size();

listaccount.add(ac1);
}

update listaccount;



}
Best Answer chosen by Dean Blanch
Yuvaraj mayilsamy 9Yuvaraj mayilsamy 9
Hi Dean,

Please copy and paste the below code. I tested it and it shows 100 percent code coverage. Mark it as the best answer if it resolves your query.
 
@istest
public class testnumberofcontacts {
    @istest static void checknumbers(){
        test.starttest();
        account testaccount = new account(name = 'Test account');
        insert testaccount;
        contact testcontact = new contact(lastname = 'Samplename', accountid = testaccount.Id);
        insert testcontact;
        test.stopTest();
        list<account> queriedaccount = [select name, number_of_contacts__C from account where id =: testaccount.Id];
        decimal resultcount;
        for(account a: queriedaccount){
            resultcount = a.Number_of_contacts__c;
        }
        system.assertEquals(1, resultcount);
    }

}

Code Coverage

Yuvaraj

All Answers

Dean BlanchDean Blanch
I have tried to validate it and come up with this error any suggestionns?

Code Coverage Failure
Your organization's code coverage is 0%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
tocountamountofcontactrecords
 
Yuvaraj mayilsamy 9Yuvaraj mayilsamy 9
Hi Dean,

Please copy and paste the below code. I tested it and it shows 100 percent code coverage. Mark it as the best answer if it resolves your query.
 
@istest
public class testnumberofcontacts {
    @istest static void checknumbers(){
        test.starttest();
        account testaccount = new account(name = 'Test account');
        insert testaccount;
        contact testcontact = new contact(lastname = 'Samplename', accountid = testaccount.Id);
        insert testcontact;
        test.stopTest();
        list<account> queriedaccount = [select name, number_of_contacts__C from account where id =: testaccount.Id];
        decimal resultcount;
        for(account a: queriedaccount){
            resultcount = a.Number_of_contacts__c;
        }
        system.assertEquals(1, resultcount);
    }

}

Code Coverage

Yuvaraj
This was selected as the best answer
Dean BlanchDean Blanch
YES YOU LEGEND TOOK ME A MONTH TO LEARN THIS PROCESS