function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gurchet Singh 11Gurchet Singh 11 

how to check the size of custom object before query fire.

Hi all,
how to check the size of custom object before query fire.When there is no record in object and we are trying select sata from that object then system return run time error(List has no rows for assignment to SObject).help to fix it.Thanks in advance
Andy BoettcherAndy Boettcher
The way that you check the record size of a custom object is through actually querying the object.  The List<sObejct> that you are filling via the query will be filled regardless if there is zero length or n length.  What you want to do is to check the List<sObject> size before applying logic based on the list.

Something to the tone of:
 
List<Account> lstAccount = [SELECT Id FROM Account WHERE....];
if(lstAccount.size() > 0) {
     logic...
}
Gurchet Singh 11Gurchet Singh 11
I know this but the error comes before "if" condition at line 1
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Gurchet

Please try below query .
List<aggregateResult> results =[SELECT COUNT(Id) FROM Widget__c where ......]
if(results.size() > 0) {
}

Let us know if it helps
Andy BoettcherAndy Boettcher
Gurchet,

Can you share your code that is generating that error?  I wouldn't advise burning another SOQL query just to get a list size before querying it again.  There is probably something else we can recommend.

-Andy