I have this trigger code to pull down a contact lookup relationship from Account to Opportunity.

Code below:

//trigger to update relationship_manager on contact based on account

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

//extract related accounts
Set<String> accIdSet = new Set<String>(); 
for(Opportunity opp: trigger.new){
Map<String, Account> accMap = new Map<String, Account>([select Relationship_Manager__c
    from Account where id in :accIdSet ]);
//set field on Opportunity
for(Opportunity opp: trigger.new){
    if(opp.AccountId!=null && accMap.containsKey(opp.AccountId) ){
        opp.Relationship_Manager__c = accMap.get(opp.AccountId).Relationship_Manager__c;

The test code is below:

public with sharing class TestRelationshipManager {
static testMethod void testOpportunities(){
    //insert records
    Contact c1 = new Contact(lastname = 'test', Nickname__c='test');
    insert c1;
    Account a1 = new Account(name = 'test1', Account_Type__c='non-Member', Industry='Banking');
    a1.Relationship_Manager__c = c1.id;
    insert a1;

    Opportunity o1 = new Opportunity(name = 'test2');
    o1.stageName = 'Target';
    o1.closeDate = System.today()+10;
    o1.AccountID = a1.id;
    insert o1;

     //check whether field is updated correctly
}  //


The error I receive is:

Error Message: System.AssertException: Assertion Failed: Expected: null, Actual: 003R000000oeLzzIAE

It should not be null and actually pulls in the contact record.


1 Answer 1


Try to query for the field in your test file. The only field that will be updated in the test data after an insert is the ID, you need to query for the other fields after the insert to see the result.

Example: o1 = [SELECT Id, Relationship_Manager__c FROM Opportunity WHERE Id=:o1.Id]; before your system assert.

  • Can you clarify? This is my first attempt at apex coding.
    – Shawn
    Sep 26, 2014 at 17:45
  • Added an example, let me know if that helps.
    – Jenny B
    Sep 26, 2014 at 17:48
  • Perfect! That's the missing code.
    – Shawn
    Sep 26, 2014 at 17:57
  • Also, great youtube video for testing best practices can be found here youtube.com/watch?v=n9amswhOxJw
    – Jenny B
    Sep 26, 2014 at 18:57

You must log in to answer this question.

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