You need to sign in to do that
Don't have an account?
Dean 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;
}
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;
}
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.
Yuvaraj
All Answers
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
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.
Yuvaraj