function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sarah Osburn 3Sarah Osburn 3 

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();
    }
}
}
Best Answer chosen by Sarah Osburn 3
Paul_BoikoPaul_Boiko
Hi Sarah,
First of all trigger needs to be before insert, because you update record that is being inserted.
so the trigger will look like this: 
trigger reassignContactOwnerToAccountOwner on Contact ( before 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 );
    }
   
}
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: 
@IsTest
private class AccountContactOwner {
	public static TestMethod void testTrigger() {
		Test.StartTest();

		//Step 1 : Data Insertion
		Account a = new Account(Name = 'Test Account');
		insert a;
		Contact c = new Contact(AccountId = a.Id, FirstName = 'John', LastName = 'Doe');
		insert c;



		//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', AccountId = a.Id);
			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();
		}
	}
}


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

Paul_BoikoPaul_Boiko
Hi Sarah,
First of all trigger needs to be before insert, because you update record that is being inserted.
so the trigger will look like this: 
trigger reassignContactOwnerToAccountOwner on Contact ( before 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 );
    }
   
}
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: 
@IsTest
private class AccountContactOwner {
	public static TestMethod void testTrigger() {
		Test.StartTest();

		//Step 1 : Data Insertion
		Account a = new Account(Name = 'Test Account');
		insert a;
		Contact c = new Contact(AccountId = a.Id, FirstName = 'John', LastName = 'Doe');
		insert c;



		//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', AccountId = a.Id);
			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();
		}
	}
}


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.

 
This was selected as the best answer
Sarah Osburn 3Sarah Osburn 3
Paul, you are a genius!  It worked perfectly and I was able to deploy in 3 minutes!  Thank you so much!