+ Start a Discussion

If statement code coverage

I am looking for some help with a simple if statement and the code coverage

here is the trigger

trigger Case4fromRD on RD_Site_Report__c (after insert, after update) {
List<Service_Call_Log__C>SCL=New list<Service_Call_Log__C>();
    For(RD_Site_Report__C b:trigger.new)
        IF (b.case_topic__C !=null){
           SCL.add(new Service_Call_Log__C(Subject__C='A Case has been created from an RD Site Report',job__C=b.Job__C, description__C=b.case_Topic__C, Web_email__C=b.contact_email__C));
            insert SCL; 

and here is the test class

public class Rd_Site_Report_SCL1{

static testMethod void CasefromRD(){

RD_Site_Report__c sRDSR = new RD_Site_Report__c();

sRDSR.Reason__C='Customer Complaint';
sRDSR.Case_Topic__c ='Test';


insert sRDSR;


James LoghryJames Loghry
What's the issue? It looks like your test method should be hitting the if statement correctly.  Did you actually run the test?  Also, keep in mind that inserts in for loops (especially triggers) are a sure fire way to hit governor limits.  Instead, you should add the records to a list, and then insert the list ouside of the for loop.
I second James on moving the insert out of the for loop. Also, on your testing, (in order to test those types of issues), I would recommend trying to insert multiple records. A good example of that is available here : https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
When i test it i am noly getting 66% coverage (4/6)
You should be able to see exactly which lines aren't being tested. but a 4/6 would indicate there are multiple methods being tested.
i moved the insert out of the loop and now it meets 75% but the line 6 which is still in the if statement is not covered

1 trigger Case4fromRD on RD_Site_Report__c (after insert, after update) {
2 List<Service_Call_Log__C>SCL=New list<Service_Call_Log__C>();
3     For(RD_Site_Report__C b:trigger.new)
4    {
5        IF (b.case_topic_4__C !=null){
6           SCL.add(new Service_Call_Log__C(Subject__C='A Case has been created from an RD Site Report',job__C=b.Job__C, description__C=b.case_Topic_4__C, Web_email__C=b.contact_email_4__C));
8      }
10  insert SCL;
11     }
12 }
I see case_topic_4__c in the IF, and just case_topic__c in the test?
James LoghryJames Loghry
Yeah, you posted the wrong code originally.  In the class you posted originally, you're checking for "case_topic__c" and you're also setting it to a value in your test class.  According to the code you last posted, you'll need to set "case_topic_4__c" to a value as well.

Also, when you post code or Visualforce markup, please do so using the code formatting button (< >).  Thanks!