Skip to main content
added note about just using Trigger.new
Source Link
Peter Knolle
  • 29.2k
  • 12
  • 104
  • 164

You should not put your SOQL inside of a for loop, because you will hit governor limits as soon as your list of Accounts becomes larger than 100. See Understanding Execution Governors and Limits for more info on governor limits.

You can use the Trigger.newMap's keySet to query for all of the Opportunities and then update them. You can take advantage of the fact that the Account that you query back as part of the Opportunity query will have the new value set at the time of the after update.

Note that you do not have to handle after insert, since it will be impossible for Opportunities to exist at the time that their parent Account is being inserted.

trigger AccountTrigger on Account (after update) {
 
    List<Opportunity> opps = [
        Select Id, Website__c, Account.Website
        From Opportunity
        Where AccountId In :Trigger.newMap.keySet()
    ];
    for (Opportunity opp : opps) {
        opp.Website__c = opp.Account.Website;
    }
    update opps;
}

Sidenote: You could also just query using Trigger.new directly and not use Trigger.newMap.keySet() as it is legal syntax to use a list of sObjects with IDs as part of an In clause. So you could have the following as your where clause. Either one will work.

Where AccountId In :Trigger.new

You should not put your SOQL inside of a for loop, because you will hit governor limits as soon as your list of Accounts becomes larger than 100. See Understanding Execution Governors and Limits for more info on governor limits.

You can use the Trigger.newMap's keySet to query for all of the Opportunities and then update them. You can take advantage of the fact that the Account that you query back as part of the Opportunity query will have the new value set at the time of the after update.

Note that you do not have to handle after insert, since it will be impossible for Opportunities to exist at the time that their parent Account is being inserted.

trigger AccountTrigger on Account (after update) {
 
    List<Opportunity> opps = [
        Select Id, Website__c, Account.Website
        From Opportunity
        Where AccountId In :Trigger.newMap.keySet()
    ];
    for (Opportunity opp : opps) {
        opp.Website__c = opp.Account.Website;
    }
    update opps;
}

You should not put your SOQL inside of a for loop, because you will hit governor limits as soon as your list of Accounts becomes larger than 100. See Understanding Execution Governors and Limits for more info on governor limits.

You can use the Trigger.newMap's keySet to query for all of the Opportunities and then update them. You can take advantage of the fact that the Account that you query back as part of the Opportunity query will have the new value set at the time of the after update.

Note that you do not have to handle after insert, since it will be impossible for Opportunities to exist at the time that their parent Account is being inserted.

trigger AccountTrigger on Account (after update) {
    List<Opportunity> opps = [
        Select Id, Website__c, Account.Website
        From Opportunity
        Where AccountId In :Trigger.newMap.keySet()
    ];
    for (Opportunity opp : opps) {
        opp.Website__c = opp.Account.Website;
    }
    update opps;
}

Sidenote: You could also just query using Trigger.new directly and not use Trigger.newMap.keySet() as it is legal syntax to use a list of sObjects with IDs as part of an In clause. So you could have the following as your where clause. Either one will work.

Where AccountId In :Trigger.new
Source Link
Peter Knolle
  • 29.2k
  • 12
  • 104
  • 164

You should not put your SOQL inside of a for loop, because you will hit governor limits as soon as your list of Accounts becomes larger than 100. See Understanding Execution Governors and Limits for more info on governor limits.

You can use the Trigger.newMap's keySet to query for all of the Opportunities and then update them. You can take advantage of the fact that the Account that you query back as part of the Opportunity query will have the new value set at the time of the after update.

Note that you do not have to handle after insert, since it will be impossible for Opportunities to exist at the time that their parent Account is being inserted.

trigger AccountTrigger on Account (after update) {

    List<Opportunity> opps = [
        Select Id, Website__c, Account.Website
        From Opportunity
        Where AccountId In :Trigger.newMap.keySet()
    ];
    for (Opportunity opp : opps) {
        opp.Website__c = opp.Account.Website;
    }
    update opps;
}