You need to sign in to do that
Don't have an account?
Trigger Test Class Not Working
I have a trigger that updates the contact owner to whoever the account owner is when the account owner is changed. I also need the trigger to allow for account teams and dataloading the account owner changes. Trigger works on a 1 for 1 basis but still has 0% coverage and test class fails. Any help is really greatly appreciated.
Trigger:
trigger reassignContactOwnerToAccountOwner on Contact ( after insert ) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
accountIds.add( c.accountId );
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
Test Class:
@IsTest
private class AccountContactOwner {
static TestMethod void testTrigger()
{
test.StartTest();
//Step 1 : Data Insertion
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(Account=a, FirstName='John',LastName='Doe');
update c;
test.startTest();
//Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above
//create test user
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='saplingstandarduser@testorg.com');
System.runAs(u1) {
// The following code runs as user 'u'
// create test contact
Contact c1 = new Contact(FirstName='Jane',LastName='Doe', Account=a);
insert c1;
//assert your results using system.assert and system.asserEquals
//reload objects to make sure values are loaded
a= [select id, ownerId from Account where id=:a.id];
test.stopTest();
}
}
}
Trigger:
trigger reassignContactOwnerToAccountOwner on Contact ( after insert ) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
accountIds.add( c.accountId );
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
Test Class:
@IsTest
private class AccountContactOwner {
static TestMethod void testTrigger()
{
test.StartTest();
//Step 1 : Data Insertion
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(Account=a, FirstName='John',LastName='Doe');
update c;
test.startTest();
//Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above
//create test user
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='saplingstandarduser@testorg.com');
System.runAs(u1) {
// The following code runs as user 'u'
// create test contact
Contact c1 = new Contact(FirstName='Jane',LastName='Doe', Account=a);
insert c1;
//assert your results using system.assert and system.asserEquals
//reload objects to make sure values are loaded
a= [select id, ownerId from Account where id=:a.id];
test.stopTest();
}
}
}
First of all trigger needs to be before insert, because you update record that is being inserted.
so the trigger will look like this: Contact needs to be inserted, not updated (line 11) and Account on Contact you need to set like AccountId = a.Id
Also there are two test.startTest();
Here is working test class:
As a common practice, if your question is answered, please choose 1 best answer.
Also don't forget to give answer a thumb up if that answer helped you.
All Answers
First of all trigger needs to be before insert, because you update record that is being inserted.
so the trigger will look like this: Contact needs to be inserted, not updated (line 11) and Account on Contact you need to set like AccountId = a.Id
Also there are two test.startTest();
Here is working test class:
As a common practice, if your question is answered, please choose 1 best answer.
Also don't forget to give answer a thumb up if that answer helped you.