function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn NguyenShawn Nguyen 

Auto-populate related contact with new Opportunity

We have a field on the Account calls Relationship_Manager__c that looks up to a contact record. We want whenever an opportunity is created, the same lookup relationship is created on the Opportunity record field calls Relationship_Manager__c. I know WF can't accomplish and that only a trigger would do. Can anyone help with the trigger and test code?
Thanks in advance!
Hargobind_SinghHargobind_Singh
If you do have more required fields on Accounts, Contacts, or Opportunities, update the test class and add those fields while creating records

Trigger:
//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; 
		}
	}
		
	
}



Test:
@isTest

public with sharing class Test1 {

	static testMethod void testOpportunities(){
		//insert records
		Contact c1 = new Contact(lastname = 'test'); 
		insert c1; 
		Account a1 = new Account(name = 'test1'); 
		a1.relationship_manager__c = c1.id; 
		insert a1; 
		
		Opportunity o1 = new Opportunity(name = 'test2'); 
		o1.stageName = 'Processing'; 
		o1.closeDate = System.today()+10; 
		o1.AccountID = a1.id; 
		insert o1; 
		
		//check whether field is updated correctly 
		Opportunity o2 = [select relationship_manager__c from Opportunity where id = :o1.id]; 
		System.assertEquals(o2.relationship_manager__c,c1.id); 
	
	}
}



If this resolves your purpose, please mark this topic as solved. 


Shawn NguyenShawn Nguyen
The trigger works when I test with real data. However the Test code returns an error. Code 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', 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);
    }
}

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