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.

Get the Reddit app

Unofficial Salesforce Developer Subreddit

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

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

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

I have taken a reference from : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm and developed below code.

But it gives me the following error.

MyController

MyControllerVF

enter image description here

My input screen.

enter image description here

  • visualforce

user4567570's user avatar

  • Its a good practice to add limit 1 to the query when getting one record, also do you actually have any record in the system that matches that Id? try pasting the record id in url and see where it takes you –  d_k Commented May 16, 2017 at 5:37
  • 2 make sure to put the ?id=<an account id> in the url when requesting the page. –  martin Commented May 16, 2017 at 5:40
  • @martin, put your comment as answer –  Santanu Boral Commented May 16, 2017 at 5:44

2 Answers 2

EDIT added upsert + exception handling

This is not going to fix the problem - while the error checking is good, it won't prevent the error mentioned here from occurring.

You shouldn't query straight to a single sObject - you need to query to a list. Eg:

Then your save method can upsert the Account.

Caspar Harmer's user avatar

You should always query in a List like:

itzmukeshy7's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged apex visualforce ..

  • The Overflow Blog
  • Community Products Roadmap Update, July 2024
  • Featured on Meta
  • We spent a sprint addressing your requests — here’s how it went
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...

Hot Network Questions

  • Plastic plugs used to fasten cover over radiator
  • Dual citizenship with USA & South Africa and exited South Africa on wrong passport (USA). What passport do I use to reenter SA?
  • Challenge the appointment of the prime minister
  • Optimizing Pi Estimation Code
  • Vacuum region in SIESTA
  • Air magic only used to decrease humidity and improve living conditions?
  • Why are responses to an attack in a cycling race immediate?
  • Where is the pentagon in the Fibonacci sequence?
  • I forgot to remove all authors' names from the appendix for a double-blind journal submission. What are the potential consequences?
  • How much does a factory reset help in hiding a device's identification details?
  • PowerShell manipulation of `for` results
  • firefox returns odd results for file:/// or file:///tmp
  • Is it possible to easily change the inclination when using momentum exchange tethers?
  • Can the US president legally kill at will?
  • ForeignFunctionLoad / RawMemoryAllocate and c-struct that includes an array
  • Are there rules for gender of durations?
  • Why was this a draw? What move I supposed to play to win?
  • Could two moons orbit each other around a planet?
  • How close would a quasar have to be to be seen with the naked eye?
  • Did Joe Biden refer to himself as a black woman?
  • Where am I wrong in my derivation of RC charging circuit equation?
  • Question about NMAP HTTP Verb Tampering
  • Simulate slow disks in KVM to see effect of LVM cache in test setup
  • Is a desert planet with a small habitable area possible?

fatal_error system queryexception list has no rows for assignment to sobject

  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

System.QueryException: List has more than 1 row for assignment to SObject

System.QueryException: List has more than 1 row for assignment to SObject Error is in expression '{!search}' in component apex:commandButton in page simpsons_vf01_car_show: Class.Simpsons_Cl1_classCar.search: line 36, column 1

David Reed's user avatar

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. –  Community Bot Commented Jan 4, 2022 at 19:42

Presumably the value sim is of type Car__c . You cannot assign the result of a query that returns anything other than exactly one record to a value whose type is an SObject. In particular, this does not make sense when you're performing a search query with a LIMIT 20 clause.

You should type your variable as a List<Car__c> .

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged salesforce soql or ask your own question .

  • The Overflow Blog
  • Community Products Roadmap Update, July 2024
  • Featured on Meta
  • We spent a sprint addressing your requests — here’s how it went
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • What makes a homepage useful for logged-in users
  • The [lib] tag is being burninated

Hot Network Questions

  • Can a country refuse to deliver a person accused of attempted murder?
  • If I Trace "Pickup", will I pickup?
  • The meaning of "奪耳" in 《說文解字》
  • Reversing vowels in a string
  • Manga/manhua/manhwa where the female lead is a princess who is reincarnated by the guard who loved her
  • Job talk Q&A: handling the room vs. being respectful
  • Mac Mini G4 not reinitialized
  • What makes Python better suited to quant finance than Matlab / Octave, Julia, R and others?
  • Visa requirements for British passport holders going to Ghana via Brussels
  • A check given by castling: is it a discovered check or a special case?
  • Do you always experience the gravitational influence of other mass as you see them in your frame?
  • What is the correct translation of the ending of 2 Peter 3:17?
  • What spells can I cast while swallowed?
  • A manifold whose tangent space of a sum of line bundles and higher rank vector bundles
  • Was I wrongfully denied boarding for a flight where the airliner lands to a gate that doesn't directly connect to the international part the airport?
  • How to solve the intersection truncation problem of multiple \draw[thick, color=xxx] commands by color?
  • Is a desert planet with a small habitable area possible?
  • Souls originating from Adam HaRishon
  • How close would a quasar have to be to be seen with the naked eye?
  • Are there dedicated research facilities in the USA?
  • Air magic only used to decrease humidity and improve living conditions?
  • Question about NMAP HTTP Verb Tampering
  • How to photograph the lettering on a bronze plaque?
  • Struggling w/ Jesus and Jewish law

fatal_error system queryexception list has no rows for assignment to sobject

IMAGES

  1. salesforce

    fatal_error system queryexception list has no rows for assignment to sobject

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

    fatal_error system queryexception list has no rows for assignment to sobject

  3. Salesforce: Visualforce System.QueryException: List has no rows for

    fatal_error system queryexception list has no rows for assignment to sobject

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

    fatal_error system queryexception list has no rows for assignment to sobject

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

    fatal_error system queryexception list has no rows for assignment to sobject

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

    fatal_error system queryexception list has no rows for assignment to sobject

VIDEO

  1. how to repair fatal error (system halted) asus p8b-x server motherboard

  2. COMO RESOLVER A MENSAGEM CHASSIS INTRUDED FATAL ERROR SYSTEM HALTED CLARO E OBJETIVO

  3. Chassis Intrude Fatal Error Hardware Fix

  4. CLASS 12 Chapter 2 Relation or a Table#ar3noor

  5. Что делать если в биосе надпись "Chassis intruded! Fatal Error… System Halted"

  6. Salesforce: System.QueryException: List has no rows for assignment to SObject in test class

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

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

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

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

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

    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. Additional Resources SEE ALSO

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

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

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

    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. Third, there should be a record in ur ...

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

    To resolve the error, manually change the Record Type of the affected Record(s). You will want to change any AMS Stage that does not match the Job Order. Information

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

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

    I am getting FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject. I know that it is because it is not returning any rows in the SOQL query but what I want is that if it returns any row, it should update ALO field on contact object, if it is null then ALO field should be set to null. Here is my code:

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

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

    Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue do the bulk of their work in the browser, Svelte shifts that work into a compile step that happens when you build your app.

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

    The code is returning one Id and getting successful. But the problem is that as soon as I hit the verify challenge in the trailhead,another log will get generated with message in the logs as " list has no rows for assignment to sObject",as shown in the screenshot above.

  16. List has more than 1 row for assignment to SObject

    List has more than 1 row for assignment to SObject. if I limit the query with one record it's working, but I wanted to get the list of records. public static List<Account> AccountsRequiringDebtCase() {. Integer creditTermDate30 = 2; Integer creditTermDate35 = 7; Integer creditTermDate45 = 17; List<Account> accList = new List<Account>();

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

    Its a good practice to add limit 1 to the query when getting one record, also do you actually have any record in the system that matches that Id? try pasting the record id in url and see where it takes you

  18. Apex エラー「List has no rows for assignment to SObject (sObject に割り当てる行が

    クエリで何も行が返されないとエラー「List has no rows for assignment to SObject (sObject に割り当てる行がリストにありません)」が発生します。 解決策 通常、SELECT は配列/リストを返しますが、これらのステートメントは 1 行のみが返されることを想定した簡略構文 ...

  19. System.QueryException: List has more than 1 row for assignment to SObject

    Presumably the value sim is of type Car__c.You cannot assign the result of a query that returns anything other than exactly one record to a value whose type is an SObject. In particular, this does not make sense when you're performing a search query with a LIMIT 20 clause.. You should type your variable as a List<Car__c>.