Salesforce Trigger To Add Contacts From Account Linked to Opportunity

I couldn’t find any example code on the Net to accomplish a task that is probably common amongst many Salesforce implementations. I have a requirement to link a second account to an opportunity in Salesforce. In my use case, the primary opportunity account represents a construction project general contractor, and the second linked account represents the building owner. I need to automatically pull the building owner account contacts into the opportunity contact roles list. Here’s the Salesforce Trigger to do just that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
trigger AssignBuildingOwnerAccountContact on Opportunity (after insert, after update) {

    System.debug(Logginglevel.DEBUG, 'Starting AssignBuildingOwnerAccountContact trigger');

    // holds all contacts
    List<OpportunityContactRole> lstOpportunityContacts = new List<OpportunityContactRole>();    
    List<AccountContactRole> lstAccountContacts = new List<AccountContactRole>();
   
    // holds building owner account
    List<Account> lstBuildingOwner = new List<Account>();
       
    // get each building owner
    for(Opportunity objOpportunity : Trigger.new) {
       
        System.debug(Logginglevel.DEBUG, 'Start Trigger loop on Opportunity' + objOpportunity.Name);
           
        // get building owner (of object type account)
        if(objOpportunity.Building_Owner__c == NULL) {
            System.debug(Logginglevel.DEBUG, 'No building owner');
          continue;
        }
       
        System.debug(Logginglevel.DEBUG, 'Got building owner ID from opportunity');
       
        lstBuildingOwner.clear();
        lstBuildingOwner = [SELECT Id FROM Account WHERE Id = : objOpportunity.Building_Owner__c];
        if(lstBuildingOwner.size() < 1) {
            System.debug(Logginglevel.DEBUG, 'No building match for id');
            continue;
        }
       
        System.debug(Logginglevel.DEBUG, 'Found building owner in database');
       
        // get all contacts associated with building owner
        lstAccountContacts.clear();
        lstAccountContacts = [SELECT ContactId FROM AccountContactRole WHERE AccountId = :lstBuildingOwner[0].Id];
        if(lstAccountContacts.size() < 1) {
            System.debug(Logginglevel.DEBUG, 'No building contacts to insert into opportunity roles');
            continue;
        }
       
        System.debug(Logginglevel.DEBUG, 'Found building owner contacts in database');
       
        // add/update building owner contacts to opportunity contact roles
        lstOpportunityContacts.clear();
        for(AccountContactRole newOppRoleContact : lstAccountContacts) {
           
          System.debug(Logginglevel.DEBUG, 'Add building owner contact to opportunity');
         
          lstOpportunityContacts.add(new OpportunityContactRole(ContactId=newOppRoleContact.ContactId, OpportunityId=objOpportunity.Id, Role='Building Owner'));
        }
        upsert lstOpportunityContacts;
       
        System.debug(Logginglevel.DEBUG, 'Save/update new building owner contacts to opportunity in database');
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *