I have a unit test that creates a test account and two test contacts. These test contacts are associated with the test account via Id. The test is for an Account trigger that will then grab all associated contacts with the account and do some logic on them. However, the created contacts don't exist when the trigger is fired.

Test file:

private class UpdateContactsOnAccountUpdate_Test {
    @isTest private static void m1()
        account a = new account();
        a.name = 'Test Account';
        insert a;

        contact c = new contact();
        c.FirstName = 'Test';
        c.LastName = 'Test';
        c.Phone = '111-111-1111';
        c.Email = 'test@test.com';
        c.AccountId = a.Id;
        insert c;

        contact c1 = new contact();
        c1.FirstName = 'Test1';
        c1.LastName = 'Test1';
        c1.Phone = '111-111-1111';
        c1.Email = 'test1@test.com';
        c1.AccountId = a.Id;
        insert c1;

        Account account = [SELECT Id, Name FROM Account WHERE Id = :a.Id];
        account.name = 'New Name';
        update account;


trigger UpdateContactsOnAccountUpdate on Account (after update) {
        account a1 = trigger.new[0];
        List<Contact> ContList = [ select Id, Name from Contact where AccountId = :a1.Id ];
        System.debug('Contlist length: '+ContList.size());
        for(contact c: ContList){
            //logic here
            update ContList;

When I run the test, the debug message says "Contlist length: 0" and the for loop is not covered by the test. How do I get the data from my test file to be accessible in the trigger?

Thank you!

  • Given code does not have any flaws so far. Any chance that there is other trigger on account/contact that removes links for newly created contacts to an account? Can you try to query your contact immediately after insert of it. Is Account Id still populated?
    – kurunve
    Apr 9, 2020 at 22:05
  • 1
    To say it has no flaws is somewhat inaccurate given the complete lack of bulkfication and assertions.
    – Adrian Larson
    Apr 9, 2020 at 22:10
  • This code is being test on a sandbox with no other data in it. When I query the contacts immediate before test.startTest(); I get both of the newly created contacts. And then in the trigger, I can log the account that fired the trigger, and the Id matches the test one that I made. I will check to see if another trigger is firing too perhaps.
    – Douglas
    Apr 9, 2020 at 22:10
  • Yes, I will be implementing bulk-ification, trigger handlers, etc. eventually. But for right now I can't seem to access my test data in my trigger, which is preventing me from testing the code inside the for loop.
    – Douglas
    Apr 9, 2020 at 22:12

1 Answer 1


Have you tried TestSetup? https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm

"Records that are created in a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution."

  • Oh this was perfect! It worked exactly as expected. Thank you!
    – Douglas
    Apr 10, 2020 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.