0

here is my code coverage picture

I think,the problem may be in account and contact connection

here is my trigger code:

    List <Contact> contacts=Trigger.New;
    for(Contact c: contacts){
        Case cs=new Case();
        cs.ContactId=c.Id;        
        if(c.AccountId!=null){ 
            List<Account> acc=[Select Id,OwnerId from Account Where Id=:c.Id];
            cs.AccountId=acc[0].id;
            cs.OwnerId=acc[0].OwnerId;            
        }
        cs.Status='Working';
        cs.Origin='New Contact';
        switch on(c.Contact_Level__c){
            when 'Primary'{
            cs.Priority='High';
            }
            when 'Secondary'{
            cs.Priority='Medium';
            }
            when 'Tertiary'{
            cs.Priority='Low';
            }
        }
        insert cs;
    }
} 

and here is my test method (methods for other Contact_Level__c value also present):

        Contact cont=new Contact();
        cont.FirstName='Pete1';
        cont.LastName='Andrews';
        cont.Phone='23456789';
        cont.Email='[email protected]';      
        cont.Contact_Level__c='Primary';
        Account acc = new Account(name = 'Super Company');
        insert acc;
        cont.AccountId=acc.Id;
        insert cont;          
        List<Case> cs=[SELECT Id,ContactId, AccountId,Priority,Status,Origin,OwnerId from Case where ContactId= :cont.Id ];         
        System.assertEquals(cs[0].Priority,'High');
        System.assertEquals(cs[0].Status,'Working');
        System.assertEquals(cs[0].Origin,'New Contact');        
        System.assertEquals(cs[0].AccountId,acc.id);
        System.assertEquals(cs[0].OwnerId,acc.OwnerId);            
        
    
1
  • 2
    Please do not delete posts that have been answered.
    – David Reed
    Nov 7, 2020 at 20:19

1 Answer 1

3

You have a bug that should be causing at least one of your unit test methods to fail, not just to miss code coverage.

List<Account> acc=[Select Id,OwnerId from Account Where Id=:c.Id];
cs.AccountId=acc[0].id;

No Account will ever have an Id equal to c.Id (you meant c.AccountId). This should result in an exception being thrown when you access the first element of this list.

As an aside, you need to bulkify this trigger to move the SOQL query out of the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .