You need to sign in to do that
Don't have an account?
Jeevana 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!
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!
Hello Christan,
Thaks for the response, I didnt getthe code coverage because I wasnt using After eventes in trigger.