0

I've written a simple trigger that updates a field on child Contact records when a specific field in the parent Account record is modified. When I test it manually, it works beautifully!

However, I'm having some issues building a test class for it. There is a specific account in our sandbox that I'm trying to use, and I try and gather the data using a SOQL query within the method, even going so far as to hard code account Id in the query, and when I run the query through the query tester it works great...but when I use the exact same query within the data method, I get an error about not having any results. What, exactly, am I missing?

@isTest
private class TriggerUnitTests {
    static testMethod void accountClientTypeTriggerTest() {
        List<Account> parent_account = [SELECT Id, Name, Client_Type__c FROM Account WHERE Id = '001Z0000002UFfa'];

        parent_account[0].Client_Type__c = 'LinkUp Prospect';
        try {
            update parent_account[0];
        } catch (System.DMLException e) {
            System.debug('Account record could not be updated.');
        }
    }
}

1 Answer 1

4

Salesforce seperates the test data and the real data. Having said if you are specific about using a specific account in the system ( good practice is to create a test account within the test class that mimics the account data you are querying for) you need to use @istest(seealldata = true).

Seealldata gives testmethods access to real data. The seealldata has many disadvantages and you may run into locking issues in the future, I would recommend that you create a new account with the exact data and try to update the account.

@isTest
private class TriggerUnitTests {
    static testMethod void accountClientTypeTriggerTest() {
        account parent_account = new account();
        parent_account. something = 'abc';
//mimic the account that you are trying to query 
        Insert parent_account;

UPDATE:

//how would I create a Contact record that is a child of an Account?
    //associate the contact's account with the parent_account created.
            contact c = new contact();
            c.accountId = parent_account.id;
            insert c;
            parent_account.Client_Type__c = 'LinkUp Prospect';
            try {
                update parent_account;
            } catch (System.DMLException e) {
                System.debug('Account record could not be updated.');
            }
        }
    }
2
  • Also, remember that tests never commit, so you even if you are using seealldata, you'll never see the results in the UI. As soon as the test class finishes, it will wipe out all changes (records created, updated, or deleted.) May 20, 2013 at 21:15
  • This is very helpful, thank you! I have one more question - Since I'm testing interaction between parent and child records, how would I create a Contact record that is a child of an Account? I'm having difficulties getting this to work.
    – Lisa
    May 20, 2013 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .