Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having trouble with what should be a simple test class for a simple trigger.

The trigger inserts the ID of an account named Retail into a lookup field on the opportunity named Project__c if that field is blank. The trigger works but the test class fails.

The error I'm getting is:-->System.AssertException: Assertion Failed: Expected: 0011100000ArLgYAAV, Actual: null

So, my test isn't updating the ID although in real application the ID is updated?

If anyone can let me know what I've done wrong it would be much appreciated.



trigger AutoRetail on Opportunity (before insert, before update) {

Account RetailAccount = [
    Select Id
    From Account
    Where Name = 'Retail'

for (Opportunity o : {

        if(o.Project__c == Null){

o.Project__c = RetailAccount.Id;

Test Class

private class AutoRetail_Test {

static testMethod void AutoRetail() {

    // Create an Account
    Account a = new account();
    a.Name = 'Test Account';
    insert a;

    // Create a 'Retail' Account
    Account r = new account();
    r.Name = 'Retail';
    insert r;

    // Create an Opportunity
    Opportunity opp = new Opportunity();
    opp.Name = 'TestOppFromTestClass' ;
    opp.StageName = 'Stage 1 - Reviewing Information' ;
    opp.CloseDate = Date.Today() ;
    opp.Accountid =;
    insert opp ;

    // Query Success

    system.AssertEquals(, opp.Project__c);


share|improve this question
up vote 5 down vote accepted

You need to query result before the assertion.

For example:

List<Opportunity> Opp=[Select Project__c FROM Opportunity WHERE ID];

share|improve this answer
Worked a treat thank you! Still learning. – user7324 Mar 12 '14 at 15:24

Try to retrieve the inserted opportunity again and then do the assertion against that.
opp = [SELECT Name, Project__c FROM Opportunity WHERE ID =: opp.Id]; System.AssertEquals(, opp[0].Project__c);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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