-2

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;
    }}

Apex Trigger:

Task

trigger taskTrigger on Task (before insert, before Update) { 
if(trigger.isInsert || trigger.isUpdate){ 
taskTrigger.Task();
}}

Case

Trigger CaseTrigger on Case (before insert,before update){ 
if(Trigger.isAfter){ if(Trigger.isInsert || Trigger.isUpdate){ 
caseClass.syncTaskOwner(trigger.newMap, trigger.oldMap);
} }

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!

| improve this question | | | | |
1

Your unit test attempts to execute the functionality under consideration by performing DML inserts and updates. However, your post does not contain - and your code coverage suggests you either do not have, or have failed to deploy - an Apex trigger.

The classes TaskTrigger and CaseClass will not be called unless an active Apex trigger is present on the relevant object and calls the class. If you do have an Apex trigger, ensure that it is active and that the DML event you desire does in fact call the class.

| improve this answer | | | | |
  • Hello, I do have an Active trigger. – Jeevana Priyanka Feb 24 at 1:36
  • Please include it in your post. – David Reed Feb 24 at 13:29
  • It worked, I am just working on the code coverage. – Jeevana Priyanka Feb 24 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.