views:

33

answers:

1

I am running a Dynamic SOQL query in my apex test code, and the LIMIT clause is not working. It does however seem to work when using it in the production code.

The Query is similar to below. I dynamically build up the where clause using some terms from a form.

string query = 'SELECT name, billingstreet, billingpostalcode, phone ';
query += 'FROM Account WHERE ';
query += '(name LIKE \'%limited%\' OR name LIKE \'%LIMITED%\') ';
query += 'LIMIT 500';

List<Account> results = Database.query(query);

System.assert(results.size() <= 500);

This can fail as the query seems to return well over 500 records in the test. The query does work however when using this in a visual force page.

Any thoughts?

A: 

I tested dynamic SOQL in the test method with a limit clause and it worked fine without any issues.

I suggest you to put some system.debug prior to assertion to check the size of the accounts list returned.

Hope this way you will come to know whats happening.

imtimran
When I system.debug the results.size() it is returning 6000+ records despite the LIMIT 500.
Xian

related questions