function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anup ramanup ram 

How to avoid soql inside for loops

Can u tell  me how i can reframe  logic to elimiate soql inside for loop. What is the best possible way.

 

I had written a logic in which a soql was placed inside a for loop. I got to know it is not a good way of implementation.

 

Please share your knowledge.

dhelmerdhelmer

You'll need to provide a more concrete example of what you're trying to accomplish.

 

If you have a list of sObjects that you're trying to update, you can format a query to pull the list back, then use a for loop to make the change, and then performing the DML statement on the loop after it finishes.

 

I'd give this a read if you'd like to know more.

Puja_mfsiPuja_mfsi

Hi,

please tell me your requirement what you have tried? 

1. If you want to query any SObject records related to some other object then put all the Id in set in for loop and the create query outer the for loop with condition :

where in :setName.

2. If you need to used the query results back to the records from your get the Ids of set then you put Id in Map corrsponding to the record.

 

you provide your condition then i can help you in good manner.

 

Thanks

Puja

Mindfire Solutions

www.mindfiresolutions.com

sushant sussushant sus
u can use concept of list set and map

for example
(account a: trigger.new)
{
s.add(a.id);
}
list<contact>con=new list<contact>([select name from contact where accountid in:s])