• Richard Stephens
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I'm using a great snippet of code created by Jake Backues in the following link: change owner without changing tasks (https://developer.salesforce.com/forums/?id=906F000000091paIAA)

The problem is now testing this.  I'm new at this and have done my best to create a test class.  The problem is, my code coverage is 0%.  Can anyone point me in the right direction on how to get the test class to test the use case?

Trigger:
trigger AccountTrigger on Account (before update, after update) {

    if(Trigger.isBefore) {
        EventUtil.setOwnerIds(Trigger.oldMap, Trigger.newMap);
    }

    if(Trigger.isAfter) {
        EventUtil.updateOwnerIds();
    }
}
Class:
public with sharing class EventUtil {

    /** Event Id => Original Owner Id **/
    public static Map<Id,Id> originalEventOwnerIds = new Map<Id,Id>();

    public static Void setOwnerIds(Map<Id,Account> oldMap, Map<Id,Account> newMap) {

        /** Only process this data if the Account (household) owner changed **/
        Id[] changedAccounts = new Id[]{};
        for(Id hhId : newMap.keySet()) {
            if(oldMap.get(hhId).OwnerId != newMap.get(hhId).OwnerId) {
                changedAccounts.add(hhId);
            }
        }

        for(Event e : [Select Id, WhatId, Subject, OwnerId, Owner.Name from Event where WhatId in :changedAccounts and StartDateTime >= TODAY]) {
            originalEventOwnerIds.put(e.Id, e.OwnerId);
        }
    }

    public static Void updateOwnerIds() {
        
        if(originalEventOwnerIds.isEmpty()) return;

        /** Grab active user list to see if the old user is still active **/
        Map<Id,User> activeUsers = new Map<Id,User>([Select Id, Name from User where IsActive = true and Id in :originalEventOwnerIds.values()]);

        Event[] eventsToUpdate = new Event[]{};
        Event[] allEvents = [Select Id, Subject, OwnerId, Owner.Name from Event where Id in :originalEventOwnerIds.keySet()];
        for(Event e : allEvents) {
            if(originalEventOwnerIds.get(e.Id) != e.OwnerId && activeUsers.keySet().contains(originalEventOwnerIds.get(e.Id))) {
                e.OwnerId = originalEventOwnerIds.get(e.Id);
                eventsToUpdate.add(e);
            }
        }

        if(eventsToUpdate.size() != 0) {
            update eventsToUpdate;
        }
    }
    
}

Test:
@isTest
public class setOwnerTest 
{
    static testMethod void inactiveOwners() 
	{
		Account a = new Account();
			a.Name = 'Test';
        	a.OwnerId = 'testuser';
		insert a;
		
		Contact cont = new Contact();
			cont.FirstName ='test';
			cont.LastName ='Demo';
			cont.accountId = a.id;
		insert cont;
		
        Test.startTest();
			Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
			User usr = new User(Alias = 'standt', Email='standarduser@testorg.com', 
						EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
						LocaleSidKey='en_US', ProfileId = p.Id, 
						TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
			insert usr ;
			System.runAs(usr)
			{
				a.ownerId = usr.id;
				update a;
			}
        Test.stopTest();       
    }
}

 
I'm using a great snippet of code created by Jake Backues in the following link: change owner without changing tasks (https://developer.salesforce.com/forums/?id=906F000000091paIAA)

The problem is now testing this.  I'm new at this and have done my best to create a test class.  The problem is, my code coverage is 0%.  Can anyone point me in the right direction on how to get the test class to test the use case?

Trigger:
trigger AccountTrigger on Account (before update, after update) {

    if(Trigger.isBefore) {
        EventUtil.setOwnerIds(Trigger.oldMap, Trigger.newMap);
    }

    if(Trigger.isAfter) {
        EventUtil.updateOwnerIds();
    }
}
Class:
public with sharing class EventUtil {

    /** Event Id => Original Owner Id **/
    public static Map<Id,Id> originalEventOwnerIds = new Map<Id,Id>();

    public static Void setOwnerIds(Map<Id,Account> oldMap, Map<Id,Account> newMap) {

        /** Only process this data if the Account (household) owner changed **/
        Id[] changedAccounts = new Id[]{};
        for(Id hhId : newMap.keySet()) {
            if(oldMap.get(hhId).OwnerId != newMap.get(hhId).OwnerId) {
                changedAccounts.add(hhId);
            }
        }

        for(Event e : [Select Id, WhatId, Subject, OwnerId, Owner.Name from Event where WhatId in :changedAccounts and StartDateTime >= TODAY]) {
            originalEventOwnerIds.put(e.Id, e.OwnerId);
        }
    }

    public static Void updateOwnerIds() {
        
        if(originalEventOwnerIds.isEmpty()) return;

        /** Grab active user list to see if the old user is still active **/
        Map<Id,User> activeUsers = new Map<Id,User>([Select Id, Name from User where IsActive = true and Id in :originalEventOwnerIds.values()]);

        Event[] eventsToUpdate = new Event[]{};
        Event[] allEvents = [Select Id, Subject, OwnerId, Owner.Name from Event where Id in :originalEventOwnerIds.keySet()];
        for(Event e : allEvents) {
            if(originalEventOwnerIds.get(e.Id) != e.OwnerId && activeUsers.keySet().contains(originalEventOwnerIds.get(e.Id))) {
                e.OwnerId = originalEventOwnerIds.get(e.Id);
                eventsToUpdate.add(e);
            }
        }

        if(eventsToUpdate.size() != 0) {
            update eventsToUpdate;
        }
    }
    
}

Test:
@isTest
public class setOwnerTest 
{
    static testMethod void inactiveOwners() 
	{
		Account a = new Account();
			a.Name = 'Test';
        	a.OwnerId = 'testuser';
		insert a;
		
		Contact cont = new Contact();
			cont.FirstName ='test';
			cont.LastName ='Demo';
			cont.accountId = a.id;
		insert cont;
		
        Test.startTest();
			Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
			User usr = new User(Alias = 'standt', Email='standarduser@testorg.com', 
						EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
						LocaleSidKey='en_US', ProfileId = p.Id, 
						TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
			insert usr ;
			System.runAs(usr)
			{
				a.ownerId = usr.id;
				update a;
			}
        Test.stopTest();       
    }
}