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.

apex handle list has no rows for assignment to sobject

Points to remember about Null Coalescing Operator

apex handle 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.

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

Child to Parent communication using LWC Events | LWC Tutorial | Lightning Events in LWC

Feed Subscribe

' border=

Featured post

Sfdx deploy tool - easily deploy metadata using sfdx cli.

apex handle 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.

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.

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

IMAGES

  1. apex

    apex handle list has no rows for assignment to sobject

  2. Understanding 'List has no rows for assignment to SObject' in a trigger

    apex handle list has no rows for assignment to sobject

  3. Salesforce: List has No Rows for Assignment to SObject: Error

    apex handle list has no rows for assignment to sobject

  4. Trailhead : Apex Integration Services System.QueryException: List has

    apex handle list has no rows for assignment to sobject

  5. Salesforce: System.QueryException: List has no rows for assignment to

    apex handle list has no rows for assignment to sobject

  6. Salesforce: System.QueryException: List has no rows for assignment to

    apex handle list has no rows for assignment to sobject

VIDEO

  1. How to create display only, file browse, hidden, listmanager and shuttle item in oracle apex

  2. Getting 6000 Damage As A Movement Player

  3. NEXT STORE UPDATE! Lunar New Year Sale

  4. Why Have So Few Apex Legends Players Noticed This?

  5. Apex Basics & Database

  6. #31 Exception Handling in Apex Part 1

COMMENTS

  1. apex

    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.

  2. 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:

  3. apex

    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 ...

  4. 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:

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

    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.

  6. 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".

  7. Document Generation Error- "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.

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

    Salesforce: System.QueryException: List has no rows for assignment to SObjectHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWit...

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

    Apex error 'List has no rows for assignment to SObject' | System.QueryException: List has no rows for assignment to SObject | Salesforce troubleshooting erro...

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

    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

  11. Can somebody explain the error 'System.QueryException: List has no rows

    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 ...

  12. Vague "List has No Rows for Assignment to SObject" Error

    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.

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

    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.