+ Start a Discussion

Governor Limit on apex executed through WebService Call

Hi all,


We a button that on click executes an apex class. There are SOQL queries and DML statements in FOR loop in that class. Hence, i wanted to know the governor limit on that? We do not have any trigger in between.


Scenario is: We have to fetch data from 2 objects and after some calculation on it, we have to insert a new record to a 3rd object.


Do you think having SOQL/DML in FOr loop will run over Governor limits? Please suggest! Shall we follow best practices?

Imran MohammedImran Mohammed

For one call transaction in Salesforce, you can have issue 100 SOQL queries, 10000 records could be fetched as result of all SOQL queries.

You can issue upto 100 DML operations.


In your scenario what i suggest is, never issue SOQL and DML inside a for loop.

It may work for a small set of records, but when it comes to bulk processing you may run into Governor limits.


What you should do is make use of List, Maps, Sets and use them effectively.

As you have to fetch data from 2 objects, then you should have two queries for each object and store the result in Lists or Maps or Sets.

Then iterate the dataset and while processing create record for 3rd object and store in a list.

Then after all processing is completed, then issue the DML statement to insert records into third object


If there is a relationship between the first two objects, you can use a single SOQL for loop and do the processing you need.


Let me know if you need any help or have any questions.


You can find about Governor Limits in Apex Documentation



Yes, i got your point.. Thanks =)

I will modify my code now by following best practices!


I have webservice program to return records from 2-3 objects. I implemented this using LIMIT and OFFSET to give user an option to query different set of records. When I ran test query in developer console with LIMIT 400 and OFFSET 0, it works fine.

But when I run this webservice using SOAPUI, it returns only 200 records. Any idea?