Search code examples
salesforceapex-codevisualforce

VisualForce Controller SOQL query prematurely limited


I have a visual force page that references my controlling class 'ESWebCaseController.cls'.

The visual force page has a field called 'Company' that runs on an SF site and is accessible by anyone when the form is submitted is used in a SOQL query like:

List account_list = [SELECT Name FROM Account WHERE Name =:company];

This query returns 0 results when company = Acme, however this same query returns 1 result when done using the Eclipse IDE SF Schema. There is defnately a company named Acme in my org.

When I remove the WHERE clause in the query, 10 Accounts are returnted, even if I set LIMIT 100, only 10 accounts are returned. These 10 accounts seem to have one thing in common and that is that they reference the same parent account and/or have a specific field ID that references the parent account.

I want to know if there is anything that would cause the above query in my controlling class to be limited outside of the Query itself.

Below are the debug logs for the Query:

20:28:32.158 (158986000)|POP_TRACE_FLAGS|[163]|01p500000009goT|ESWebCaseController|APEX_CODE,FINEST;APEX_PROFILING,FINEST;CALLOUT,FINEST;DB,FINEST;SYSTEM,FINEST;VALIDATION,FINEST;VISUALFORCE,FINEST;WORKFLOW,FINEST
20:28:32.159 (159879000)|SOQL_EXECUTE_BEGIN|[163]|Aggregations:0|select Name from Account where Name = :tmpVar1
20:28:32.159 (159893000)|LIMIT_USAGE|[163]|SOQL|1|100
20:28:32.159 (159898000)|LIMIT_USAGE|[163]|AGGS|0|300
20:28:32.177 (177286000)|SOQL_EXECUTE_END|[163]|Rows:0
20:28:32.177 (177308000)|LIMIT_USAGE|[163]|SOQL_ROWS|0|50000
20:28:32.177 (177324000)|HEAP_ALLOCATE|[163]|Bytes:4
20:28:32.177 (177337000)|HEAP_ALLOCATE|[163]|Bytes:0
20:28:32.177 (177411000)|HEAP_ALLOCATE|[163]|Bytes:4
20:28:32.177 (177441000)|VARIABLE_SCOPE_BEGIN|[163]|account_list|LIST<Account>|true|false
20:28:32.177 (177488000)|VARIABLE_ASSIGNMENT|[163]|account_list|{"serId":1,"value":[]}|0x14cace14
20:28:32.177 (177504000)|STATEMENT_EXECUTE|[165]

Solution

  • Are you using with Sharing on your controller and do you have access to the account Acme from the user running the query?

    By default classes have "with sharing" so they respect the sharing in the parent org, this means that if the user who accesses the page does not have access to the data the data will not be available for the query.

    A quick and easy test for this is to get the Id of the record you need access to and log into salesforce then go to the url directly.

    So if the record was a01adf20123032 (that's a made up id fyi) and your instance was na1 you'd log into salesforce and navigate to https://na1.salesforce.com/a01adf20123032 if you get insufficient privileges then the person doesn't have access.

    Through the IDE you're most likely using a system administrator user with the "view all data" permission so they'll always get all data returned.