Print this page

Error "Number of records exceeds limit: 200" in Apex

Knowledge Article Number 000181928
Description There is an apex code that contains the following code

LIST allRecordIds = new LIST(this.IdCache);
SET userRecordAccessSet = new SET( [SELECT RecordId FROM UserRecordAccess WHERE UserId=:UserInfo.getUserId() AND HasReadAccess = true AND RecordId IN :this.IdCache LIMIT 200] );

For any reason, this code sometimes throws an exception caused by: System.QueryException: Number of records exceeds limit: 200 But the SOQL query says LIMIT 200. What throws this error? How to prevent it? 
Resolution If the list contains more than 200 records, the error will occur. So, in this case, make sure the following part doesn't contain more than 200 records.

RecordId IN :this.IdCache


promote demote