15 votes
Accepted

How do I bulkify queries for directly or indirectly related records?

It is common to have collections of records to process, especially when considering triggers (though there are many other places where collections of records may require processing together). When ...
13 votes
Accepted

Is it advisable to avoid bulkification in a trigger on attachment object?

Even if you are 100% sure that there is no possibility you will not have 200 attachments to modify, you also have to think that other code might execute in the same execution context and if the code ...
Mihai Neagoe's user avatar
  • 3,192
11 votes
Accepted

How to bulkify a trigger that is having a method to call a future method?

Your trigger is bulkified, so would make one call to the @future method for every 200 records that are loaded. As long as your call-out method can handle lists, and it appears that it can based on the ...
crmprogdev's user avatar
  • 40.9k
10 votes
Accepted

Bulkification of SingleEmailMessage

It should instead be: List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); for Opportunity record : records) { Messaging.SingleEmailMessage email = ...
Adrian Larson's user avatar
  • 150k
10 votes
Accepted

How to publish/consume bulkified Platform Events

My original answer, while useful is not the full story. After an extended discussion on the Platform Events Success Community with the Product Manager, it turns out as a trigger (or for that matter, ...
cropredy's user avatar
  • 70.9k
9 votes
Accepted

How do processes consume SOQL queries?

It's one DML and SOQL per 200 records or a single DML "chunk." To illustrate this, consider the following code: contact[] c = [select id from contact limit 5]; id accountid = [select id from account ...
sfdcfox's user avatar
  • 487k
9 votes

How to publish/consume bulkified Platform Events

In V42.0, SFDC added a new REST composite collection resource that is bulkified and works with Platform Events! /services/data/v42.0/composite/sobjects { "allOrNone" : false, "records" : [{ ...
cropredy's user avatar
  • 70.9k
7 votes
Accepted

Trigger Best Practices

First, you should try to follow the DRY pattern; by moving some logic around, we can reduce your code by almost half: trigger Order_PopulateGallery on RH_Order__c (before insert, before update){ ...
sfdcfox's user avatar
  • 487k
7 votes
Accepted

Update records using map values

You can do it like this: // Your map Map<Id, String> m = new Map<Id, String>(); // Update needs to know the type so create objects of the right type Opportunity[] updates = new ...
Keith C's user avatar
  • 136k
7 votes
Accepted

How to remove this soql query

You should always use collection like Map and Set to hold values which you will need later while for-loop iteration. Something like that: Group Tccqueue = [SELECT Id, name FROM Group WHERE type='...
Ashwani's user avatar
  • 22.6k
7 votes

Bulkify Apex Trigger when Launching Flow

The system never optimizes triggers or flows of its own accord. You will need need to gather all the relevant ID values and then call the flow. However, you should be wary of using flows in bulk ...
sfdcfox's user avatar
  • 487k
6 votes
Accepted

Choosing Bulk Count in test classes

(What follows may contain bits of heresy..... and applies mostly to non managed package use cases) The reason to do bulk testing (> 100 records) is to ferret out bad bulkification practices in the ...
cropredy's user avatar
  • 70.9k
6 votes

Is it advisable to avoid bulkification in a trigger on attachment object?

There is no Harm in adding this bulkification. NEVER say never....you could find the need to do a mass upload of attachments in the future, or another admin could do it as well. ESPECIALLY since ...
Chris Duncombe's user avatar
6 votes
Accepted

Bulkified generation of unique 6-character alphanumeric IDs, with strict validation criteria

Instead of thinking about the system as a single number, consider the system as a set of 6 smaller numbers. This lets you get rid of most of the naivety of a brute force algorithm. Here's the version ...
sfdcfox's user avatar
  • 487k
6 votes
Accepted

How many times trigger will be invoked if I want to process 30K records?

Yes, you will get the 101 query exception1. The reason is that all of the trigger "chunks" (I prefer using "chunk" over "batch", because "batch" in Salesforce is a separate thing) are part of the ...
Derek F's user avatar
  • 61k
5 votes
Accepted

Bulkify the code?

Try this. Instead of directly inserting the opportunity. Add them to a list and insert the list after the for loop. List<Opportunity> newOppList = new List<Opportunity>(); List < ...
sfdcFanBoy's user avatar
  • 4,054
5 votes
Accepted

custom setting in Apex Trigger

Your code does not look like it will have any bulk issues. You do need to check if the setting is null though, since you are not guaranteed to have a CountryInfo__c record for a given key. ...
Adrian Larson's user avatar
  • 150k
5 votes

Help to Bulkify a Trigger

Like I said in my comment, the key to bulkifying code in Salesforce is to remove all DML and SOQL from the inside of loops. The reason why this is important is because you're only allowed 100 SOQL ...
Derek F's user avatar
  • 61k
5 votes

Bulkify Apex Trigger when Launching Flow

You DO NOT want to be calling a flow 200 times from within a for loop in a trigger in the case that 200 Manager_Contact_Relationship__c records are deleted at once. Assuming your flow does any data ...
Aaron P.'s user avatar
  • 1,667
5 votes
Accepted

Does this trigger have the right approach/best practices?

If you google "apex trigger best practices", the very first article you will find is titled Trigger Frameworks and Apex Trigger Best Practices, and you are ignoring a few more or less universally ...
Adrian Larson's user avatar
  • 150k
5 votes
Accepted

Bulkify Apex Methods Using Collections In Methods false positive?

Seems like a false positive. In most cases a method that accepts a single record and performs a DML Operation on it might be called in a loop, and hence be considered non-bulk-safe. However, ...
Adrian Larson's user avatar
  • 150k
5 votes

Bulkify SOSL query in loop

To bulkify the method, SOSL should be outside of for loop. Correct syntax for joining field is (refer documentation): FIND {"Joe Smith" OR "Joe Smythe"} IN Name Fields RETURNING ...
Santanu Boral's user avatar
5 votes
Accepted

Bulkification help with apex called from trigger

Here is an approach to handle query out of for loop. public static void populateWorkOrderAddress(List<WorkOrder> newWorkOrder){ Set<Id> relatedAddIds = new Set<Id>(); for(...
Santanu Boral's user avatar
5 votes
Accepted

Bulkify Scheduled Apex Class

You need to jump over from a scheduled class to a batch class. The easiest way to do so is to add the Batchable interface atop what you already have: global class ScheduledRecurringUpdateOnContact ...
sfdcfox's user avatar
  • 487k
5 votes
Accepted

Can I remove this for loop within a for loop?

The only time you need to avoid a for loop within a for loop is when it results in a multiplicative effect. Bad Idea Account[] accounts = [SELECT Name FROM Account]; Contact[] contacts = [SELECT ...
sfdcfox's user avatar
  • 487k
5 votes

Can I remove this for loop within a for loop?

Your pattern does not appear to present any serious problems so far as efficiency or governor limits. However, you can indeed flatten the structure. The basic change you need to make is to filter ...
Adrian Larson's user avatar
  • 150k
5 votes

How to bulkify Flow call from Apex

Calling start() on a flow interview object within a loop is NOT bulkified - even if all the flow interview objects are for the same flow. This has to be because the start() method executes ...
cropredy's user avatar
  • 70.9k
5 votes

How to bulkify Flow call from Apex

Winter '23 enables Apex to bulk invoke Flows - many interviews, one transaction. The capability comes with the Invocable.Action classes. The invoke method can mass run any invocable step or indeed ...
Matt and Neil's user avatar
5 votes
Accepted

Hitting governor limits and getting the "Apex CPU time limit exceeded"

There's a lot going on in this code, but I'll start with some of the basics. First, use Test.startTest() before you insert the leads. This gives you more CPU time for you to test with. However, it's ...
sfdcfox's user avatar
  • 487k
4 votes
Accepted

How to Query for dupe leads in Bulk with Mapping?

You can use aggregate queries for this. Set<String> leadEmails = new Set<String>(); for (Lead record : trigger.new) leadEmails.add(record.Email); Map<String, Integer> ...
Adrian Larson's user avatar
  • 150k

Only top scored, non community-wiki answers of a minimum length are eligible