+ Start a Discussion
VarunCVarunC 

StandardSetController with Database.QueryLocator Problem - Is this a Bug?

Well Not sure if anybody else has faced this isuse ...

 

I'm facing it right now ... 

 

I'm using this kind statement to Populate StandardSetController instance:

 

ApexPages.StandardSetController setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([Select ID, Account__c, Contact__c, Contact__r.AccountID From Matter__c]));
setCtrl.setPageSize(400);

 

Now if Query Returned records then it works fine. But if Query doesn't return records then when i access records using this statement:

 

setCtrl.getRecords() - This returns ALL records, if QueryLocator is Empty from the query used. Why is this So happening, I think If it is Empty then It Should return 0 records not All records ... :( ...

 

Best Answer chosen by Admin (Salesforce Developers) 
wsmithwsmith
I see something similar where I get the Lead records owned by the User running the statement even though the OwnerId is part of the SOQL filter to retrieve another User's records.  It seems to act as if there are no records, return the records owned by the User running the statement.  In my case, the User running the statement is a manager (by profile and role) of the User that owns the records I am querying.
 I found a workaround from here: http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=17035
 
The code I use to have was:
conListView = new ApexPages.StandardSetController(Database.getQueryLocator(query));
conListView.setPageSize(PAGEINATIONSIZE);
which would return me the Leads owned by the user running the code, when the result set should have been 0, even though the SOQL filter explicitly set the OwnerId.
 
I changed the code to:
conListView = new ApexPages.StandardSetController(Database.getQueryLocator(query));
if (conListView.getResultSize() > 0) {
    conListView.setPageSize(PAGEINATIONSIZE);
}
and now it works as expected.
 
Message Edited by wsmith on 10-26-2009 10:52 AM
Message Edited by wsmith on 10-26-2009 12:12 PM
Message Edited by wsmith on 10-26-2009 09:02 PM

All Answers

VarunCVarunC
no one able to reproduce it ? .... anyone knows why this is happenning ... ?
wsmithwsmith
I see something similar where I get the Lead records owned by the User running the statement even though the OwnerId is part of the SOQL filter to retrieve another User's records.  It seems to act as if there are no records, return the records owned by the User running the statement.  In my case, the User running the statement is a manager (by profile and role) of the User that owns the records I am querying.
 I found a workaround from here: http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=17035
 
The code I use to have was:
conListView = new ApexPages.StandardSetController(Database.getQueryLocator(query));
conListView.setPageSize(PAGEINATIONSIZE);
which would return me the Leads owned by the user running the code, when the result set should have been 0, even though the SOQL filter explicitly set the OwnerId.
 
I changed the code to:
conListView = new ApexPages.StandardSetController(Database.getQueryLocator(query));
if (conListView.getResultSize() > 0) {
    conListView.setPageSize(PAGEINATIONSIZE);
}
and now it works as expected.
 
Message Edited by wsmith on 10-26-2009 10:52 AM
Message Edited by wsmith on 10-26-2009 12:12 PM
Message Edited by wsmith on 10-26-2009 09:02 PM
This was selected as the best answer
VarunCVarunC
Sorry picked your answer late :) .. But Thanks for the solution. So ultimately its the setPageSize() statement that is causing the issue here .. point noted .. Thanks again ...