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 - 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.
Points to remember about Null Coalescing Operator
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.
Learn how to implement lightning datatable in LWC from scratch. Implement all features from basic to advanced level.
Recognized by Apex Hours as Top Salesforce Blog
$50 OFF with LIFETIME ACCESS. Complete Salesforce Platform & Lightning Experience Training Pack
LWC Training
Competitive Programming Solutions
Subscribe for free tutorials
- @rahulcoder
Feed Subscribe
Featured post
Sfdx deploy tool - easily deploy metadata using sfdx cli.
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.
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. To enable users to access the files in the Document Template Library, you must add them as members. Or, you can create a group, grant access to the group, and add users to that group.
- Make sure to deactivate and reactivate the Document Template
- If there are custom fields in the Content Version object, marked as required, the error occurs. The managed package code will not update custom fields and they need to be marked required only at the Page Layout level.
- For DOCX Templates, the ContentVersionId will be set for the affected Document Template. Make sure this record is shared with the Docgen Document Template Library to avoid any errors.
- Open the active template in the Document Template tab (NOT the Docgen Document Template
- Add ContentVersionId field to the layout if it is not present already
- Go to the Id in Salesforce to check Sharing Settings.
- If the ContentVersionId file is not shared, share it manually with the library/user.
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.
IMAGES
VIDEO
COMMENTS
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.
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:
System.QueryException: List has no rows for assignment to SObject. Ask Question Asked 3 years, 7 months ago. Modified 3 years, 7 months ago. Viewed 3k times ... Apex. public class UpdateListofOppty { public Opportunity opportunities {get;set;} public Id recId{get;set;} public UpdateListofOpporunity(ApexPages.StandardSetController sc){ recId ...
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:
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.
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".
Make sure this record is shared with the Docgen Document Template Library to avoid any errors. Steps to fetch ContentVersionId: Open the active template in the Document Template tab (NOT the Docgen Document Template. Add ContentVersionId field to the layout if it is not present already. Go to the Id in Salesforce to check Sharing Settings.
Salesforce: System.QueryException: List has no rows for assignment to SObjectHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWit...
Apex error 'List has no rows for assignment to SObject' | System.QueryException: List has no rows for assignment to SObject | Salesforce troubleshooting erro...
If it doesn't, you will get the 'List has no rows for assignment to SObject' exception again. Reply reply More replies. Sammy-Speed ... No. Leaving out the 'limit 1' is fine if the query returns exactly one record - apex will let you skip the whole array thing. Reply reply
Im new to apex, I was trying for code coverage of atleast 75% on my batch apex code through test class. ... System.QueryException: List has no rows for assignm... Stack Overflow. About; Products For Teams; Stack Overflow Public questions & answers; ... List has no rows for assignment to SObject ". Execute method is not covering in the code ...
List<sObject> // [SELECT Id FROM Contact LIMIT 100] (returns a list of 100 Contacts) sObject // [SELECT Id FROM Contact LIMIT 1] (returns a single Contact) Integer // [SELECT Count() FROM Contact] (returns how many Contacts exist in your org) There is a gotcha here, that you ran up against.
Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.