SFDCian – Best Salesforce Consultant & Architect

Apex error – ‘List has no rows for assignment to SObject’

The error “List has no rows for assignment to SObject” occurs when query doesn’t return any rows.

Resolution   

While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned. What’s not obvious is that it also assumes that exactly one row is returned! Although this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create, especially when a WHERE statement is used that might return zero rows, such as: 

The above code will fail if there is no Player__c record with the matching username. It doesn’t actually return a null.  It would be safer to do the following:

It’s one of those situations for which you would not normally think of creating a test, so it’s safer to just avoid the possibility.

Related Posts

Salesforce: opportunity clone for each opportunity contact role along with opportunity products, are you building lightning app with locker services.

SFDC Stop

SFDC Stop - Always the latest about Salesforce

Full tutorial series with videos, free apps, live sessions, salesforce consulting and much more..

  • _Platform Event Toast
  • _HTTP Callout Framework
  • _SFDX Deploy Tool
  • _SObject Convertor
  • Live Sessions
  • _TDX18GG - Salesforce UI API
  • _TDX18GG - Lightning Testing Service
  • _DF17GG - Lightning Data Service
  • Tutorial Series
  • _JavaScript Tutorial Series
  • _Salesforce LWC Tutorial Series | ToDo App Project
  • _Salesforce Integration Tutorial Series
  • _Salesforce Lightning Events Tutorial Series
  • _Salesforce Lightning Tutorial Series
  • Discussions
  • _Salesforce Development
  • _Ideas for upcoming posts
  • Become an Author

Monday 22 January 2024

Solution for "system.queryexception: list has no rows for assignment to sobject" - null coalescing operator () | spring'24 release.

sfdc list has no rows for assignment to sobject

Points to remember about Null Coalescing Operator

sfdc list has no rows for assignment to sobject

About Rahul Malhotra Rahul Malhotra is currently working as an Application Engineer at Google . He has delivered various live technical sessions at International and National Salesforce Conferences including TrailheaDX India 2019, Dreamforce 2018, IndiaDreamin 2018, Jaipur Developer Fest 2018 as well as other global gatherings of Salesforce. Connect with him on Connections App . Username: rahulmalhotra

No comments:

Post a comment, mastering lightning datatable in salesforce lwc.

Mastering Lightning Datatable in Salesforce LWC

Learn how to implement lightning datatable in LWC from scratch. Implement all features from basic to advanced level.

Connections - Store all your professional contacts without auto sync with any other apps

Connections - Store all your professional contacts without auto sync with any other apps

Connections helps users to store all their business/professional contacts. None of the contacts are automatically synced with any other applications like: whatsapp, installed on the device.

Connect with me on connections using my username: rahulmalhotra

Recognized by Apex Hours as Top Salesforce Blog

Recognized by Apex Hours as Top Salesforce Blog

$50 OFF with LIFETIME ACCESS. Complete Salesforce Platform & Lightning Experience Training Pack

$50 OFF with LIFETIME ACCESS. Complete Salesforce Platform & Lightning Experience Training Pack

LWC Training

LWC Training

Competitive Programming Solutions

Competitive Programming Solutions

Subscribe for free tutorials

  • @rahulcoder

Custom validation in Lightning Web Component | Understanding Regular Expressions (Regex)

Feed Subscribe

' border=

Featured post

Sfdx deploy tool - easily deploy metadata using sfdx cli.

sfdc list has no rows for assignment to sobject

Report Abuse

Blog archive.

  • Affiliate Disclaimer
  • Privacy Policy
  • Contribute to SFDC Stop | Become an Author
  • What do you want to see next on SFDC Stop ?

Total Pageviews

Recent comments.

Error ‘List has no rows for assignment to SObject’ in Salesforce CPQ

The error 'List has no rows for assignment to SObject' occurs when an invalid quote process ID exists on the Quote's  Quote Process ID  field. This field is meant to be populated by automation.  This issue can also occur when the user attempts to reconfigure a Primary Quote without Read access to the associated Opportunity. Lack of access to the Opportunity object throws List has no rows for assignment to SObject' since our code queries for the Primary Quote on that opportunity.

Company Logo

Cookie Consent Manager

General information, required cookies, functional cookies, advertising cookies.

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings. Privacy Statement

Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.

Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.

Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.

Cookie List

COMMENTS

  1. Apex error 'List has no rows for assignment to SObject'

    What's not obvious is that it also assumes that exactly one row is returned! Although this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create, especially when a WHERE statement is used that might return zero rows, such as:

  2. I'm getting "List has no rows for assignment to SObject" error on a

    If the query doesn't return any rows you will get the "List has no rows for assignment to SObject" exception. Instead, assign the results to a list of sObjects and check the size of the list. Then only use the first record in the list if it is present.

  3. List has no rows for assignment to SObject

    If you get more than 1 row and attempt to assign that to your Accountx variable you will get the opposite of your original problem - System.QueryException: List has more than 1 row for assignment to SObject! So one way of guarding against this would be to use Account accountx = [SELECT Id FROM Account LIMIT 1]'. - frup42.

  4. custom object

    This is resulting in no records being returned and Salesforce throwing the System.QueryException: List has no rows for assignment to SObject. Note that this is unlike some other programming languages where you may expect the query to just set your sObject to null. Salesforce's documentation for System.QueryException states it is thrown when ...

  5. salesforce

    Impossible. If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. This eliminates getProductsLov(unless you didn't post whole code) because there you assign to a list.. Humo(u)r me and System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); in your constructor before firing that query...

  6. Apex error

    What's not obvious is that it also assumes that exactly one row is returned! Although this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create, especially when a WHERE statement is used that might return zero rows, such as:

  7. apex

    The reason for the failure is 'System.QueryException: List has no rows for assignment to SObject' which I have tried to look up but there are so many answers and they don't apply to my certain case. Tried it multiple ways and I am wondering if anybody here has a good answer. I have two objects, Implementation (custom) and Opportunity.

  8. Solution for "System.QueryException: List has no rows for assignment to

    The query which we're performing is returning a list which has no rows (no records) to assign to the contactRecord variable which is of type Contact sObject. So, we can say "List has no rows for assignment to SObject".

  9. Help with error: List has no rows for assignment to SObject

    Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site

  10. System.QueryException: List has no rows for assignment to SObject

    Salesforce Software industry Information & communications technology IT sector Technology Business Business, Economics, and Finance comments sorted by Best ... If it doesn't, you will get the 'List has no rows for assignment to SObject' exception again. Reply

  11. System.QueryException: List has no rows for assignment to SObject

    3 Answers. I would agree with Akash here. First, the 'account' instance must be initialised. Second, just make sure the ID is being passed in the page's URL correctly. In the ApexPages.currentPage ().getParameters ().get ('id') statement, the .get ('id') part is case sensitive. So please make sure, that is how you have passed your ID in the URL.

  12. System.QueryException: List has no rows for assignment to SObject. How

    The class being tested is expecting an Id, but your test is not passing an Id. The issue is on the last line of code in your test class: ItemSetupNewVersionController.itemSetupClone('is.ParentId__c'); enclosing things in single quotes makes it a string, so you're literally passing "is.ParentId__c" to the method you're testing (instead of the id of the record).

  13. Document Generation Error- "List has no rows for assignment to SObject

    Make sure the affected user has 'Salesforce CRM Content User' enabled. This must be checked on their User Detail page in Setup. The affected user should be provided with access to the Docgen Document Template Library.

  14. Docusign Custom Button Error: List has no rows for assignment to SObject

    The script is failing because DocuSign envelope Id has been set to 961202FA-36C6-4A9A-B061-EDC7BE6C8B07. The DSIED should be set to '0' by default. Set DSIED=0 and the script should work:

  15. Error 'List has no rows for assignment to SObject' in Salesforce CPQ

    General Information. We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply.

  16. FATAL_ERROR System.QueryException: List has no rows for assignment to

    System.QueryException: List has no rows for assignment to SObject 1 Salesforce Controller Extension Testing: System.QueryException: List has no rows for assignment to SObject