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){
if(opp.AccountId!=null){
accIdSet.add(opp.AccountId);
}
}
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:
@isTest
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
System.assertEquals(o1.relationship_manager__c,c1.id);
} //
}
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.
Thanks!