You need to sign in to do that
Don't have an account?
harish reddy 50
How to write testclass for trigger
Below trigger counts no of contacts and updates on noofcontacts__c on account .Now i need to write testclass for this
As i am new to apex can someone help me with this .
Thanks in advance.
CODE:
trigger countofcontacttrigger on Contact (after insert) {
LIST<ID> ids=new LIST<ID>();
for(contact conn:trigger.new){
ids.add(conn.accountid);
}
LIST<ACCOUNT> acc=[SELECT noofcontacts__c,(SELECT id from contacts) from account where id in:ids];
LIST<ACCOUNT> acc1=new LIST<ACCOUNT>();
for(account acc2:acc){
acc2.noofcontacts__c=acc2.contacts.size();
acc1.add(acc2);
}
update acc1;
}
As i am new to apex can someone help me with this .
Thanks in advance.
CODE:
trigger countofcontacttrigger on Contact (after insert) {
LIST<ID> ids=new LIST<ID>();
for(contact conn:trigger.new){
ids.add(conn.accountid);
}
LIST<ACCOUNT> acc=[SELECT noofcontacts__c,(SELECT id from contacts) from account where id in:ids];
LIST<ACCOUNT> acc1=new LIST<ACCOUNT>();
for(account acc2:acc){
acc2.noofcontacts__c=acc2.contacts.size();
acc1.add(acc2);
}
update acc1;
}
All Answers