function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
j-ferj-fer 

Can anyone assist with code coverage for the trigger below

I can't seem to get any coverage on this trigger and I am suppose to be going live with it in a few hours.  Any assitance would be greatly appreciated.
 
trigger populateCaseContact on Support_Comment__c (before insert, before update){

    set<Id> caseIdSet = new set<Id>();
    for(Support_Comment__c supCom: trigger.new){
        if(supCom.case__c != null){
            caseIdSet.add(supCom.case__c);
        }
    }

    map<id, case> caseMap = new map<id, case>([SELECT id, contactid, ownerid, owner.type, createdbyid, temporary_assignee__c from case where Id IN: caseIdSet]);

    for(Support_Comment__c supCom: trigger.new){
        if(caseMap.containsKey(supCom.case__c)){
            supCom.Case_Contact__c = caseMap.get(supCom.case__c).contactId;
            supCom.Case_Temporary_Assignee__c = caseMap.get(supCom.case__c).temporary_assignee__c;
            supCom.Case_Created_By_User__c = caseMap.get(supCom.case__c).createdbyid;
            
        if(caseMap.get(supCom.case__c).owner.type != 'Queue'){
             supCom.Case_Owner__c = caseMap.get(supCom.case__c).ownerId;
             }
        }       
    }

}

 
Bryan JamesBryan James
I recreated your class and object structure as best I could from your code. This is the test method I made and ran that got full coverage hope it helps. The only thing I dont have in here is the Case_Temporary_Assignee__c. Im sure that you should be able to rework the code here to add it in.
Hope this helps some.

@isTest
public class populateCaseContactTest {
    static testmethod void myTest(){
        Contact con = new Contact(FirstName = 'test',LastName ='test');
        insert con;
        Case casetest = new Case(ContactId = con.Id,Subject='test');
        insert casetest;
        
        Support_Comment__c sc = new Support_Comment__c(Name='test',Case__c = casetest.Id);
        insert sc;
        
        Support_Comment__c scq = [Select Case_Contact__c,Case_Created_By_User__c,Case_Owner__c, From Support_Comment__c where Id =: sc.Id];
        System.assertEquals(con.Id, scq.Case_Contact__c);
        System.assertEquals(UserInfo.getUserId(), scq.Case_Created_By_User__c);
        System.assertEquals(UserInfo.getUserId(), scq.Case_Owner__c);
    }
}