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.

Common fix to a "Error: List has no rows for assignment to SObject" error received when Rejecting Applications. Oct 25, 2018 • Knowledge

Error when rejecting applications: list has no rows for assignment to sobject, information.

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

Apr 26, 2023 • knowledge, information.

In this example, if the query doesn't find an Account with the name 'NonExistentAccount', it will throw the "System.QueryException: List has no rows for assignment to SObject" error.  

To handle this situation, you can use a List<SObject> variable to store the query results and check if any records are returned before proceeding. Here's how you can do it:

Article Details

Need more help.

Check out more articles below

  • Number of Views 3.37K
  • Number of Views 2.05K
  • Number of Views 36
  • Number of Views 2K
  • Number of Views 1.95K
  • Number of Views 1K
  • Number of Views 7.63K
  • Number of Views 191
  • Number of Views 143
  • Number of Views 145

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. System.QueryException: List has no rows for assignment to SObject

    exception list has no rows for assignment to sobject

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

    exception list has no rows for assignment to sobject

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

    exception list has no rows for assignment to sobject

  4. Getting "System.QueryException: List has no rows for assignment to

    exception list has no rows for assignment to sobject

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

    exception list has no rows for assignment to sobject

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

    exception list has no rows for assignment to sobject

VIDEO

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

  2. Salesforce: System.QueryException: List has no rows for assignment to SObject (3 Solutions!!)

  3. Salesforce: System.QueryException: List has no rows for assignment to Sobject Error?

  4. Salesforce: Error: List has more than 1 row for assignment to SObject (2 Solutions!!)

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

  6. Salesforce: System.QueryException: List has no rows for assignment to SObject (3 Solutions!!)

COMMENTS

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

  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. System.QueryException: List has no rows for assignment to SObject

    Or maybe it's being passed but it's useless. If you have StandardSetController the Id value in the page URL might be used as filter id (listview), not an id of any particular record. Meaning query will still return 0 rows. What were you trying to do? Maybe just removing that part of condition will help although this list can grow very fast.

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

    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.

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

  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. Common fix to a "Error: List has no rows for assignment to SObject

    Applications (and the other AMS Stages) has a Master-Detail Object Relationship within the Salesforce Data model to Job Order. This means the Record Type of the AMS Stages is acquired from the Job Order.

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

    To handle this situation, you can use a List<SObject> variable to store the query results and check if any records are returned before proceeding. Here's how you can do it:

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

    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.

  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. ... Having one record gets you 100% coverage and doesn't throw exceptions or have multiple exits. Again, not a huge deal in this particular case but good habits are worth a lot more than lazy, thoughtless ones.

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

    this code gives me an exception "Object reference not set to an instance of an object" when i try to execute this sql query 0 Exception raised if the table has no rows

  12. testing

    Here is what i tried. I think the problem is somewhere in the test class when i create the apex test controller and i want to link the record of the controller to the solution that i created in ...