Activity Forums Salesforce® Discussions System.Query Exception: List has no rows for assignment to s-object?

  • shariq

    Member
    September 10, 2018 at 2:43 pm

    Hi,

    The exception is because of you are not getting any record in your SOQL query which meet the where criteria so it's give error. When attempting to fetch a single record from the database it is easy to run into the above exception. To avoid such exception use try-catch box like below :

    MyObject__c obj;

    try{

    obj = [SELECT id FROM MyObject__c WHERE name=:previouslyDefinedVar];

    } catch(System.QueryException e){

    // Perform logic here

    }

    Hope this helps!

     

  • Parul

    Member
    September 10, 2018 at 6:26 pm

    Hi

    It means your SOQL didn't return any results. You can wrap it in a try/catch statement to handle this gracefully.

    This error occurs when query doesn't return any rows.

    For e.g.:- Account a = [select id, name from Account where name = 'test'];

    Instead of using this we should use like that:-

    List<Account> lstAccount  = [select id, name from Account where name = 'test'];
    if (lstAccount.size() > 0)
    {
    Account a = lstAccount.get(0);
    }

     

     

    Thanks

  • Avnish Yadav

    Member
    September 11, 2018 at 7:08 am

    Hello Prachi,

    Well, this type of error shown when your SOQL didn't return any results. You can wrap it in a try/catch statement to handle this gracefully.

    For more information, kindly share your code.

    Thanks.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos