I'm new to salesforce (apex - trigger) development and I'm currently reading through the manual and other threads on here on how to do this stuff, but I wanted to put this out here in case it was a simple fix someone would notice right away. If not, maybe some leads or pointers would even help. This is code I'm picking up from another developer.

trigger updateAllOpportunities on Opportunity (after insert, after update, after delete) {

  List<Opportunity>   triggerOpps = Trigger.isDelete ? Trigger.old : Trigger.new;
  for (Opportunity triggerOpp : triggerOpps) {
    if (triggerOpp.Parent_Opp__c != null) {
       // Add this Site Opp's properties to DOPP's site configuration.
       List<Opportunity> opps = [Select Id, Name,
            (select Id, 
                Account.Name  from Opportunities__r) 
              from Opportunity where Id = :triggerOpp.Parent_Opp__c];

       Opportunity dopp = opps.get(0);
       List<Opportunity> sopps = dopp.Opportunities__r;
       Decimal totalPositions = 0;

       for (Opportunity sopp : sopps) {
            totalPositions += sopp.NumberofPositions__c == null ? 0 : sopp.NumberofPositions__c;

       dopp.NumberofPositions__c = totalPositions;  
       update dopp;

1 Answer 1


is it possible to convert to formula field. Opportunities__r.NumberofPositions__c.

Follow Trigger best practices.

1) One Trigger Per Object A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.

3) Context-Specific Handler Methods Create context-specific handler methods in Trigger handlers

4) Bulkify your Code Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail.

  • Thank you, this helped out a lot. Especially number 4, 5, and 8. I would like to expand on number 4 that I was able to achieve this bulk update by adding my opportunity to a list, and then outside the for loop updating that list of opportunity. I believe this will then only cost 1 cycle of dml.
    – simplyzeke
    Commented Oct 18, 2016 at 16:32

Not the answer you're looking for? Browse other questions tagged .