+ Start a Discussion
KyoKyo 

Code Coverage 41% I can add it to me?

Not Error 

 

trigger CaseMasterSync on Case (before insert,before update) {
List<Case_Master__c> cm = new List<Case_Master__c>();

for(case ca : Trigger.new){
    if(ca.Case_Category__c != 'Z - Customer Care' && ca.Sub_Category__c != null ){
        
        cm = [select id,Case_Category__c,Case_Escalation_To__c,Description__c,Function__c,Priority__c,Queue__c,Queue_Name__c,SLA_Time_Hours__c,Status__c,Sub_Category__c,Text_Note__c,Type__c from
                Case_Master__c Where Case_Category__c =: ca.Case_Category__c and Sub_Category__c =: ca.Sub_Category__c and Description__c =: ca.Description__c and Customer_Class__c =: ca.Customer_Class__c];
        
        for(Case_Master__c cmt : cm){
                if(cmt.Function__c != null){ca.Function__c = cmt.Function__c;}
                if(cmt.Priority__c != null){ca.Priority = cmt.Priority__c;}
                if(cmt.SLA_Time_Hours__c != null){ca.SLATimeHours__c = cmt.SLA_Time_Hours__c;}
                if(cmt.Text_Note__c != null){ca.Text_Note__c = cmt.Text_Note__c;}
                if(cmt.Status__c != null){ca.Status = cmt.Status__c;}
                if(cmt.Type__c != null){ca.Type = cmt.Type__c;}       
                if(cmt.Case_Escalation_To__c != null){ ca.OwnerId = cmt.Case_Escalation_To__c;}
               
                
        }
    }   
}
}

 

@isTest 
private class TestCaseMasterSync {
    static testMethod void myTest() {
    
          
        Account Acc = new Account();
        Acc.Name = 'test'; 
        Acc.AccountNumber = '000001';
        insert Acc;
        update Acc;
     
        Contact con = new Contact();
        Con.AccountID = Acc.id;
        Con.Email = 'Test@Email.com';
        Con.Email_2__c = 'Test2@Email.com';
        Con.LastName = 'Test';
        insert Con;
        update Con;
        
        Profile p = [select id from profile where name='Standard User'];
        User ua = new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
            insert ua; 
        
        Case_Master__c cm = new Case_Master__c();     
        cm.Type__c = 'Inquiry';
        cm.Status__c = 'Open 2nd Level';
        cm.Case_Category__c = 'CIPI';
        cm.Sub_Category__c = 'ISCC - Open ISCC Account';
        cm.Description__c = 'Request open ISCC account';
        cm.Customer_Class__c = 'Platinum';
        insert cm;
        update cm;
        
        Case ca = new Case();     
        ca.Account_Number__c  = '000001';
        ca.AccountID = Acc.id;
        ca.ContactID = con.id;
        ca.SuppliedEmail = 'Test@Email.com';     
        ca.Origin = 'Phone';      
        ca.Case_Category__c = 'CIPI';
        ca.Sub_Category__c = 'ISCC - Open ISCC Account';
        ca.Description__c = 'Request open ISCC account';
        ca.SLATimeHours__c = cm.SLA_Time_Hours__c ;
        ca.Text_Note__c = cm.Text_Note__c ;
        ca.Priority = cm.Priority__c ;
        ca.Function__c = cm.Function__c ;
        ca.Status = cm.Status__c ;
        ca.OwnerID = ua.id ;
        ca.Type = cm.Type__c;
        insert ca;
        Test.Starttest();
        update ca;
        Test.Stoptest();    
        }
        }

 Thank you so much.

Bramha1Bramha1

Hi,

 

 First of all just remove all the update statements immediatly after the insert statemnt. I think you are not doing any thing after the insert right.

And when you come to your code coerage, in the trigger you are checking with Case_Master__c if the new Case is already existing and you havent covered it in ur Test Class.

 

1) Try to insert 2-3 Case_Master__c

2) Before creating the Case check for exsting records as you did in ur trigger.

 

 

Guess that will work.

 

 

Cheers

 

Bramha