You need to sign in to do that
Don't have an account?
Iyappan Kandasamy 8
Need test class for the update triggers...and the insert trigger..
Hi...Actually I have written 2 triggers and I need test class for these two triggers...
1. Trigger to update contact description whenever account description is updated with the same value of account description...
trigger acc on account(after update)
{
set<id> accids = new set<id>();
for(account a : trigger.new)
{
accids.add(a.id);
}
list<contact> conlist=[select id,account.description, description from contact where accountid=: accids];
for (contact c: conlist)
{
c.description=c.account.description;
}
update conlist;
}
2.) Whenever account is created then create contact with the account details ...
trigger createcon on account(after insert)
{
list<contact> conlist = new list<contact>();
for (account a:trigger.new)
{
contact c = new contact();
c.lastname= a.name;
c.accountid=a.id;
conlist.add(c);
}
insert conlist;
}
Kindly need test classes for the above 2 triggers.
Any help on writing the test classes would be of great help....Thanks...
Thanks in advance...
1. Trigger to update contact description whenever account description is updated with the same value of account description...
trigger acc on account(after update)
{
set<id> accids = new set<id>();
for(account a : trigger.new)
{
accids.add(a.id);
}
list<contact> conlist=[select id,account.description, description from contact where accountid=: accids];
for (contact c: conlist)
{
c.description=c.account.description;
}
update conlist;
}
2.) Whenever account is created then create contact with the account details ...
trigger createcon on account(after insert)
{
list<contact> conlist = new list<contact>();
for (account a:trigger.new)
{
contact c = new contact();
c.lastname= a.name;
c.accountid=a.id;
conlist.add(c);
}
insert conlist;
}
Kindly need test classes for the above 2 triggers.
Any help on writing the test classes would be of great help....Thanks...
Thanks in advance...
- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)
----------------Test Class-------------------
@isTest public class createcon_Test {
@isTest public static void tset1()
{
Test.startTest();
Account acc=new Account();
acc.Name='Test';
insert acc;
update acc;
Test.stopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
You are correct to question the quality of the trigger. Whilst it provides "coverage" , it provides no assurance that your code is operating correctly.
We can roll the two triggers into one test. I would just annotated both triggers to describe which test class provides coverage:
/** *This trigger * Test Class - createContact_Test */
Test class below provides coverage and asserts. (not it has not been compiled).
Note also, for best practice, you should have one trigger per object. And the bundle the trigger activities in to a handler class (as best practice).
The reason is that for multiple triggers, if they have the same event, you cannot control the order in which they fire.
Regards
Andrew
Yes, it is for both the triggers.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.