I have a few thousands Contacts and a few thousands Accounts. I need to update a custom field of all my Accounts with Emails of related Contacts. In my flow I get "Error Occurred: Too many SOQL queries: 101". How can I fix it?
A SOQL query is any query to Salesforce where you're trying to lookup a record. Think of it kind of like when you do a search in the Salesforce search bar for a record. That is 1 query. (Although technically that's a SOSL query... but that's not relevant).
When you build a Flow, anytime to do a "Record Lookup" or "Fast Lookup", that'd be 1 query.
In Process Builder, if you have any Criteria or Actions that drill through to other objects (like... you're Process runs on Opportunity Records, but you use criteria that looks up to fields on the parent Account to determine what to do), then I suspect the Process is doing a SOQL query to go get those fields on the Account record.
So basically, anytime your Flow or Process needs to go fetch data from a field/record, it's likely doing a SOQL query.
Flows are subject to governor limits and similar to Apex, you need to ensure that the Flows respect those limits otherwise there can be consequences. Be it auto-launched flows or screen flows, both should be designed in a manner where they only query, create or update the needed records and do not perform a superfluous action. Listed are a few ways via which you can improve Flow optimization:
Always prefer the Fast elements over Record elements(Fast Lookup, Fast Create, Fast Update)
A Record Lookup or Record Update or Record Create element should be avoided inside a Loop element at all costs. Let’s assume a scenario where you have to do a bulk update on Opportunity Line Items within a Flow. One way is to loop through the Opportunity Line Items collection and update each element within the Loop using a Record Update element. But that would be bad design. Reason? If someone performs a bulk upload of 200 records using Data Loader and that triggers our Flow to update the corresponding Line Items, the upload would fail with the “Too many DML statements: 151” error. And this is why we must use Assignment elements to store each updated Opportunity Line Item in a collection variable and then save the bulk update to be handled outside the Flow by using a Fast Update element.
Use Decision elements to only Update when needed
Reference:- BULKIFY YOUR FLOW
Another helpful document to understand Sample Flow That Loops Through a Collection:-