1

Can someone please elaborate what is mapAccounts.get(opp.AccountId) is getting here MapAccount acc = mapAccounts.get(opp.AccountId);

trigger OppTrigger on Opportunity (after insert) {   
    if(Trigger.isInsert && Trigger.isAfter)
    {
        Set<Id> acctSet = new Set<Id>();
        List<Account> accList = new List<Account>();
        for(Opportunity opp : Trigger.new)
        {
            acctSet.add(opp.AccountId);
        }

        Map<Id, Account> mapAccounts = new Map<Id, Account>([SELECT Id, accField__c FROM Account where Id IN :acctSet]);
        for(Opportunity opp : Trigger.new)
        {
            Account acc = mapAccounts.get(opp.AccountId);
            acc.accField__c = opp.oppField__c;
            accList.add(acc);
        }
        Update accList;
    }

}

there is no error in this trigger but i need to understand in detail, what mapAccounts.get(opp.AccountId); is getting here

1

Here is what happens here:

1) Create acctSet to get unique account ids, because opps and accs has many-to-one relation.

2) Select accounts in the mapAccounts, where key is accountId and related value is the actual account record.

3) Then we go again through all opportunities in triggerAccount acc = mapAccounts.get(opp.AccountId); this line gets from map the actual account, which id corresponds with the opp.AccountId.

  • Thank You, Problem is Cleared now :) – Dave Jul 20 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.