You need to sign in to do that
Don't have an account?
darcusmietz
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) { AccOwnerMap.put(a.Id,a.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; ContactsToUpdate.add(c); } } if(ContactsToUpdate.size() > 0) { update ContactsToUpdate; } }
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.
Account acc = new Account(name = 'Test', ownerId = user1.Id);
insert account;
//below assignment will cover your code
account.OwnerId = User2.Id;
update account;
accoun
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:
account.OwnerId = '00540000001lRFu';
with
a.OwnerId = '00540000001lRFu';
If it is working then modify the code to assign the owner id instead of hard coding the id values.