You need to sign in to do that
Don't have an account?
shakila G
How to write a test class for my new Trigger code?
I wrote a trigger to prevent task creation if customer Account is blocked.
This is my code,
trigger EventBlocked on Event (Before insert,after update) {
list<Contact> clist =new list<Contact>();
string Cname;
for (Event t : Trigger.new) {
if (t.WhoId !=Null) {
clist= [select ID, Account_name__C, Account.Lock_record__C from Contact where
ID=:t.WhoId and Account.Lock_record__C=True];
}
IF(Clist.size()>0)
{
T.adderror('You Cant create a Event under the blocked Contact');
}
}
}
How to write a test class for this trigger to improve the code coverage?
This is my code,
trigger EventBlocked on Event (Before insert,after update) {
list<Contact> clist =new list<Contact>();
string Cname;
for (Event t : Trigger.new) {
if (t.WhoId !=Null) {
clist= [select ID, Account_name__C, Account.Lock_record__C from Contact where
ID=:t.WhoId and Account.Lock_record__C=True];
}
IF(Clist.size()>0)
{
T.adderror('You Cant create a Event under the blocked Contact');
}
}
}
How to write a test class for this trigger to improve the code coverage?
I hope this helps:
For more Info on test class, you can visit this blog:
https://webkul.com/blog/test-classes-in-apex-salesforce/
Regards,
Aakanksha Singh
Thank you so much.
Regards
Shakila
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
sample Test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Always add Assert in your test class to validate the trigger result
NOTE:- Even dnt use SOQL inside the for loop. try to update your trigger like below'
Let us know if this will help you