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
Jeevana Priyanka BJeevana Priyanka B 

Why is my code coverage 0% in the Sandbox, To stop automatic transfer of open activity Ownership related to account/case I have created a trigger on task and cases which calls an apex class.

Apex class: 
Task
Public class taskTrigger{
public static void Task(){
        for(Task t: (List<Task>) Trigger.new){
             t.Custom_ownerText_field__c = t.OwnerId;
}
}
}
Case
public class caseClass{
    public static void syncTaskOwner( Map<Id, SObject> oldParents, Map<Id, SObject> newParents ){
            Set<Id> changedOwnerIds = new Set<Id>();
            // Identify all Parents that have a changed Owner
            for( SObject parent : newParents.values() ){
                SObject oldParent = oldParents.get((Id)parent.get('Id'));
                
                if( parent.get('OwnerId') <> oldParent.get('OwnerId') ){
                    changedOwnerIds.add((Id)parent.get('Id'));
                }
            }
            
            // Find all the tasks (repeat for Events if required) that are not closed for the parent records that had new Owners
            List<Task> restoreTaskOwner = new List<Task>();
            
            // revert the owner on the found tasks
            for(Task tsk : [SELECT Id, OwnerId, Custom_ownerText_field__c  FROM Task Where WhatId IN :changedOwnerIds AND IsClosed = FALSE AND Last_Assigned_User__c <> null]){
                System.debug('tsk.OwnerId..' + tsk.OwnerId);
                System.debug('tsk.Custom_ownerText_field__c ..' + tsk.Last_Assigned_User__c);
                if (tsk.OwnerId <> tsk.Custom_ownerText_field__c ) {
                    tsk.OwnerId = tsk.Custom_ownerText_field__c ;
                    restoreTaskOwner.add(tsk);
                }
            }
            if(!restoreTaskOwner.isEmpty())
            update restoreTaskOwner;
        }
}

I have created 2 different test classes both has 0% code coverage.
Test Class
Test Class 1:
@isTest
public class TestClass1{
    @isTest
    static void TestM1(){
        Account acc = new Account(Name = 'Test Acc');
        Insert acc;
        Case ca = new Case(Name__c= 'Test', origin = 'web', Status = 'New');
        Insert ca;
        Task ta = new Task();
        ta.Type = 'Email';
        ta.Status = 'New';
        ta.Description = 'Test SA';
        ta.OwnerId = userinfo.getUserId();
        ta.WhatId = ca.Id;
        ta.Custom_ownerText_field__c = userinfo.getUserId();
        Insert ta;
        
        Profile p = [SELECT Id FROM profile WHERE Name = 'System Administrator'];
        User u = new User(ProfileId = p.Id , Alias = 'Test', Username = 'Test', Email='Test@Test.com', EmailEncodingKey = 'UTF-8', LastName= 'Test', 
                          LanguageLocaleKey = 'en_US', LocaleSidKey = 'en_US', TimeZoneSidKey='America/Los_Angeles');
        ta.ownerId = u.Id;
        update ta;
        ca.ownerId = u.Id;
        update ca;
    }
}
Test Class 2:

@isTest
public class TestClass2{
@TestSetup
    static void setup(){
        Account newAcct = new Account(
            Name = 'Test'
        );
        insert newAcct;
        
        Case newCase = new Case(
            Subject = 'test'
            , Status = 'new'
        );
        insert newCase;
        
        Task newTsk = new Task(
            //WhatId = newAcct.Id
            WhatId = newCase.Id
            , Subject = 'Test'
        );
        insert newTsk;
    }
    
    @IsTest
    static void syncTaskOwnerTest(){
        Task[] existingTasks = [
            SELECT Id, OwnerId, Custom_ownerText_field__c , WhatId
            FROM Task
            Where IsClosed = FALSE AND Custom_ownerText_field__c <> null
        ];
        System.assert( existingTasks.size() == 1, existingTasks );
        System.assertEquals( existingTasks[0].Custom_ownerText_field__c , UserInfo.getUserId() );
        System.assertEquals( existingTasks[0].Custom_ownerText_field__c , existingTasks[0].OwnerId );
        
        User[] usr = [SELECT Id FROM User WHERE IsActive = true AND Id != :UserInfo.getUserId() LIMIT 1];
        
        Test.startTest();
        update new Case(
            Id = existingTasks[0].WhatId
            , OwnerId = usr[0].Id
        );
        Test.stopTest();
        
        existingTasks = [
            SELECT Id, OwnerId, Custom_ownerText_field__c , WhatId
            FROM Task
        ];
        Account acct = [SELECT Id, OwnerId FROM Account LIMIT 1];
        system.assertEquals( usr[0].Id, acct.OwnerId );
        System.assert( existingTasks.size() == 1, existingTasks );
        System.assertEquals( existingTasks[0].Custom_ownerText_field__c , UserInfo.getUserId() );
        System.assertEquals( existingTasks[0].Custom_ownerText_field__c , existingTasks[0].OwnerId );
    }
}

Plese let me know if you have any questions.

Thank you in advance!
Christan G 4Christan G 4
Hi Jeevana, I hope you are well. The reason your test classes are failing is you never called any of the methods created within it. Also, you mentioned that you created two triggers but all I see is code you have written for two Apex classes which is separate. In your test class, you not only have to test the code within your trigger but also the code within your methods.
Jeevana Priyanka BJeevana Priyanka B
Well I have created Trigger handler which is calling these methods, But I really didnt get what you said.
Christan G 4Christan G 4
Hi Jeevana, you basically have to call you the methods you created in your test class. Lets say for example that you wanted to test, the method: public static void syncTaskOwner( Map<Id, SObject> oldParents, Map<Id, SObject> newParents). You will have to create a map of oldParents and a map of newParents in your test class. Then, pass these as parameters within the method. When testing the method, you should have something like caseClass.syncTaskOwner(oldMap,newMap); between your Test.startTest() and Test.stopTest(); You'll need to do this for all the methods you wrote.
Jeevana Priyanka BJeevana Priyanka B

Hello Christan,

Thaks for the response, I didnt getthe code coverage because I wasnt using After eventes in trigger.