Feature NewsSalesforce

Using Triggers to add Related Records

Adding Related Records

We can use triggers to add related records to an object. In the coming example, the trigger will add a related opportunity for each and update the account if no opportunity is already associated with the account.

Let’s do it step by step.

  1. We will create our trigger on Account. It will work after the insert and after the update. We need a list to keep all the opportunities.
trigger AddRelatedRecord on Account(after insert, after update) {
    List<Opportunity> oppList = new List<Opportunity>();

2. The trigger first performs a SOQL query to get all child opportunities for the accounts that the trigger fired on.

// Get the related opportunities for the accounts in this trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);

We will store the query inside a map that contains the key and value. In this SOQL we are selecting child opportunities inside of the bracket coming after the first SELECT Id.

3.At this point, we need to add a loop that the trigger iterates over the list of sObjects in Trigger.New to get each account sObjects. If the account does not have any related opportunity sObjects, the for loop creates one. If the triggers created any new opportunities, the final statements inserts them.

// Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(new Opportunity(Name=a.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=a.Id));
        }           
    }
if (oppList.size() > 0) {
        insert oppList;
    }
}

You can see the final of our trigger below.

trigger AddRelatedRecord on Account(after insert, after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    
    // Get the related opportunities for the accounts in this trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(new Opportunity(Name=a.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=a.Id));
        }           
    }
    if (oppList.size() > 0) {
        insert oppList;
    }
}

Leave a Reply

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