+ Start a Discussion

Test Class for Account Owner Map

I'm having trouble writing a text class to cover the AcctOwner Map of my trigger. I'm missing coverage on the boldface lines. Is there something special I need to do in order to get coverage for a map and key?


trigger UpdateContactOwnerFromAccount on Account (after update) {
	map<Id,Id> AccOwnerMap = new map<Id,Id>();
	for(Account a : trigger.new) {
		if(a.OwnerId != trigger.oldMap.get(a.Id).OwnerId) {
	list<Contact> ContactsToUpdate = new list<Contact>();
	for(Contact c: [select Id, OwnerId, AccountId 
	from Contact where AccountId IN :AccOwnerMap.keySet()]) {
		if(AccOwnerMap.containsKey(c.AccountId)) {
			Id AccOwnerId = AccOwnerMap.get(c.AccountId);
			c.OwnerId = AccOwnerId;
	if(ContactsToUpdate.size() > 0) {
		update ContactsToUpdate;



Kiran  KurellaKiran Kurella

No, you don't need special code to cover a map and key. 


For your trigger, you just need to update the ownerId of an existing Account to get the test coverage. If you are still having issues then I recommend you to post your test method to get some help.

Bhawani SharmaBhawani Sharma
Are you changing teh ownerId in your test method? You should do like
Account acc = new Account(name = 'Test', ownerId = user1.Id);
insert account;

//below assignment will cover your code
account.OwnerId = User2.Id;
update account;


That's definitely the logic I was looking for. I'm a little confused, though. I keep getting "Variable does not exist" errors. I even tried hardcoding the Owner Ids… 


Here is my test code:


@isTest (SeeAllData = true) 
public with sharing class TestPrimaryContactLeadSrc {
 static testMethod void TestPrimaryContactLeadSrc() 
               Account a = new Account(
                Name = 'testing in test method',
                OwnerId = '00540000001jrPO');
                insert a;
                account.OwnerId = '00540000001lRFu';
                update account;
               Contact newContact = new Contact( lastname= 'Testerson',
                                        LeadSource = 'Cold Call',
                                        AccountId = a.id
                insert newContact;
               Opportunity o = new Opportunity (Name = 'testopp',
                                                    Set_up_Fee__c = 10, 
                                                    Term__c = 1,
                                                    CARR__c = 1,
                                                    StageName = 'Meeting',
                                                    ForecastCategoryName = 'Pipeline',
                                                    CloseDate = date.today(),
                                                    Type = 'Upsell',
                                                    LeadSource = 'Sales',
                                                    AccountId = a.Id
               insert o;
               OpportunityContactRole ocr=new OpportunityContactRole(Role='Decision Maker',OpportunityId=o.Id,ContactId=NewContact.Id,Isprimary=true);
     insert ocr;      
               o.Set_up_fee__c = 9;
update o;   


Kiran  KurellaKiran Kurella
You don't have account variable. Replace the following line

account.OwnerId = '00540000001lRFu';


a.OwnerId = '00540000001lRFu';

If it is working then modify the code to assign the owner id instead of hard coding the id values.