• 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 no rows for assignment to SObject

I want update list of opportunities using visualforce page.In visualforce page I want to update Amount,close date and Stage.The visulaforce page should only show all open opportunities(STAGE NOT EQUALS TO CLOSED WON AND CLOSED LOST).

Anyone help me to fix this error and update only all open opportunities.

Here is my code:

Error shown in this line:

  • visualforce

Caconde's user avatar

I suspect your id parameter is not passed through so it's a null. Querying Opportunities WHERE Id = null will always return zero results. 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. You might want to put a LIMIT statement in the query or read about StandardSetController and pagination.

eyescream's user avatar

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 apex visualforce or ask your own question .

  • The Overflow Blog
  • Scaling systems to manage all the metadata ABOUT the data
  • Navigating cities of code with Norris Numbers
  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites
  • Tag hover experiment wrap-up and next steps

Hot Network Questions

  • Why is "a black belt in Judo" a metonym?
  • Examples of flat projective morphisms with non-divisorial branch locus
  • What are those bars in subway train or bus called?
  • What counts as a pet?
  • Am I allowed to link code licensed under GPL to proprietary libraries?
  • A post-apocalyptic short story where very sick people from our future save people in our timeline that would be killed in plane crashes
  • How can sound travel as a transverse wave?
  • "Not many can say that is there."
  • Dropper post won't actuate. Is this fixable?
  • Are there rules of when there is linking-sound compound words?
  • 3D cartoon involving a lion with a crown on his head, a crystal, and artifacts that need to be found
  • Justification for the Moral Ought
  • Getting straight single quotes in a bibliography
  • Why does editing '/etc/shells' file using 'sudo open' show an error saying I don't own the file?
  • Is "the above table" more acceptable than "the below table", and if so, why?
  • What is the lowest feasible depth for lightly-armed military submarines designed around the 1950s-60s?
  • Why did evolution fail to protect humans against sun?
  • Transit from intl to domestic in Zurich on Swiss
  • Can you continue a database log restore after putting an interim restore online?
  • Why are these simple equations so slow to `Solve`?
  • Automotive Controller LDO Failures
  • What tool has a ring on the end of a threaded handle shaft?
  • can a CPU once removed retain information that poses a security concern?
  • What are these on my cabbages?

list has no rows for assignment to sobject salesforce apex

Salesforce – List has no rows for assignment to SObject

if you using following code in Apex and Error should throw like “List has no rows for assignment to SObject”.

Account a = [Select name from acoount where id =: strId];

Try to avoid the error following code in you Apex.

List<Account> lstAccount = [Select Id, name from acoount where id =: strId];

list has no rows for assignment to sobject salesforce apex

Sakthivel Madesh

3x Salesforce MVP | Platform Champion | 22x Salesforce Certified | MuleSoft Certified | All Star Ranger | TechForce Services | Sydney | Australia

What are the Granting Access available in Salesforce Event Calendar

How to return a pagereference to create a sobject in salesforce, you may also like, upgrading apex classes for custom metadata access: a..., congratulations to the 2024 class of salesforce mvps,..., unlock your salesforce certification with free vouchers in..., free salesforce certification days webinars – december 2023, spring 24 – dynamic forms supported the related..., salesforce flow – repeater (beta) screen components –..., salesforce flows & reactive screen components: the next..., free salesforce certification days webinars – november 2023, join the salesforce community sprint: accelerating innovation together, elevate your salesforce journey with free accredited professional..., leave a comment cancel reply.

Save my name, email, and website in this browser for the next time I comment.

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.

list has no rows for assignment to sobject salesforce apex

Points to remember about Null Coalescing Operator

list has no rows for assignment to sobject salesforce apex

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.

list has no rows for assignment to sobject salesforce apex

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.

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.

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

EDIT: Is there a recommended way for a VF email template that uses a VF component to display in the SFDC UI such that you don't get the error message "Error occurred trying to load the template for preview: List has no rows for assignment to SObject. Please try editing your markup to correct the problem"

when saving the template? As the original problem description states below, I get this message when saving the VF template. The only workaround (see component controller code) that seems to work for me is to read a "test" opportunity such that the VF template has something to display, but this seems to be a complete hack - and I'm not guaranteed to have a "test" opportunity in any environment.

ORIGINAL: I'm trying to use a component in a VF email template that queries opportunity line items from an opportunity lookup in a custom object. I get the error "List has no rows for assignment to SObject" if I don't query at least one opportunity object, which is the basis of my question.

The gist of the VF template is this;

The Component looks like this;

And the controller is this;

But what I don't like is I'm burning an extra SOQL statement just for preview of the VF page. If strOptID is null (during preview) I have to query an "artificial" opportunity that "should" be there ("%Test%").....

I don't like this solution and I'm sure there's a better way, and I was hoping someone might point me to a more appropriate way to accomplish this.

Thanks in advance.

  • visualforce
  • visualforce-component

Matt Lacey's user avatar

  • 1 possible duplicate of Help with error: List has no rows for assignment to SObject –  Daniel Ballinger Commented Mar 2, 2015 at 18:31
  • 4 I've voted to close this question as the general error has been covered previously in other questions. As a general rule, don't assign the results of a SOQL query to a singular sObject. 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. –  Daniel Ballinger Commented Mar 2, 2015 at 18:33
  • Hi all, that link doesn't cover components - I vote that it's a different problem. I've searched "VF Component" and "No Rows" and this is the only question. –  user2223 Commented Mar 2, 2015 at 18:37
  • 1 I don't believe the exception differs because it is a component. Can you indicate which line in your example code is throwing the exception? Maybe you would like to rework your question to emphasis that that the issue is getting a default record in preview mode rather than the resulting exception. –  Daniel Ballinger Commented Mar 2, 2015 at 18:47
  • 2 The problem is doing this in Apex: Opportunity lo = [SELECT Id, Name FROM Opportunity WHERE Id = :strOptID LIMIT 1]; . You need to assign the SOQL results to a List<Opportunity> , check the resulting List size and then get lo from the first record in the list. If the list is empty you need to return. –  Daniel Ballinger Commented Mar 2, 2015 at 18:56

3 Answers 3

General rule - don't return result of SOQL to an object, but return it to a list of objects. After querying items, check the list. Don't do

In your case Opportunity. But you have two places where this error may appear.

Andrii Muzychuk's user avatar

I would suggest that you refactor your queryDetailData method to query into a list instead of a single record. Then, if the resulting size of the List is 0, then set a boolean rendered flag to false, thus hiding the component, and preventing the error from occurring when you are in preview mode.

JimRae's user avatar

  • Thanks JimRae. Great idea about the rendered flag, but since it's an email that's related to one record, (Limit 1), can't I still just check for the return opportunity being null after the SOQL? Thanks again. –  user2223 Commented Mar 2, 2015 at 21:39
  • You could, but you may still have the no results error. The other way to handle this would be to put the SOQL query in a try/catch block, then catch the Query Exception and as long as it is "no rows" related, you could set the no render flag, or handle the optional processing another way of your choosing. –  JimRae Commented Mar 2, 2015 at 22:19

tempOppty is a single sObject. May be the soql is not assigning any record to this. You would want to convert this into a list and proceed accordingly.

the_phantom's user avatar

You must log in to answer this question.

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

  • The Overflow Blog
  • Scaling systems to manage all the metadata ABOUT the data
  • Navigating cities of code with Norris Numbers
  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites

Hot Network Questions

  • How is delayed luggage returned to its owners from a location with infrequent flights?
  • Why don't programming languages or IDEs support attaching descriptive metadata to variables?
  • Why are the perfect fifth and fourth called "perfect" in 12-ET when they differ by approximately 2 cents from just intonation?
  • Is there an "ESA Astronaut Alexander Gerst Observatory" in Künzelsau, Germany? If so, what's it like?
  • Are the US or its European allies offering Iran anything in return for de-escalation?
  • Decent 900 MHz radome material from a hardware store
  • Using "where" to modify people
  • Why are these simple equations so slow to `Solve`?
  • How can one design earplugs so that they provide protection from loud noises, such as explosions or gunfire, while still allowing user to hear voices?
  • Could a 3D sphere of fifths reveal more insights than the 2D circle of fifths?
  • Communicate the intention to resign
  • A burning devil shape rises into the sky like a sun
  • Does full erase create all 0s or all 1s on the CD-RW?
  • Can this Integer Linear Programming problem be solved in polynomial time?
  • Claims of "badness" without a moral framework?
  • Why does editing '/etc/shells' file using 'sudo open' show an error saying I don't own the file?
  • Automotive Controller LDO Failures
  • How to declutter a mobile app home screen with a floating “Create Video” component?
  • What are these on my cabbages?
  • Suitable tool bag for vintage centre pull brake bike
  • Sharing course material from a previous lecturer with a new lecturer
  • Would several years of appointment as a lecturer hurt you when you decide to go for a tenure-track position later on?
  • In zsh, annotate each line in a file to which both stdout and stderr have been redirected with the line's source (stdout or stderr)
  • Can I continue using technology after it is patented

list has no rows for assignment to sobject salesforce apex

IMAGES

  1. apex

    list has no rows for assignment to sobject salesforce apex

  2. Salesforce: Getting List has no rows for assignment to SObject error (4

    list has no rows for assignment to sobject salesforce apex

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

    list has no rows for assignment to sobject salesforce apex

  4. Salesforce: List has no rows for assignment to SObject

    list has no rows for assignment to sobject salesforce apex

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

    list has no rows for assignment to sobject salesforce apex

  6. User Apex Error

    list has no rows for assignment to sobject salesforce apex

COMMENTS

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

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

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

  4. custom object

    SOQL queries can be used to assign a single sObject value when the result list contains only one element. When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list. For example: List<Account> accts = [SELECT ...

  5. apex

    System.QueryException: List has no rows for assignment to SObject Asked 3 years, 11 months ago Modified 3 years, 11 months ago Viewed 3k times

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

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

  8. Errors in Apex Class

    I want to add an apex message inside of my method that will showup if query returns a null value. Here is a bit of the code that includes query. AccName = [Select Name, Customer_Account_Number__c ...

  9. Salesforce: List has no rows for assignment to SObject

    For a deeper understanding of this error and further help with troubleshooting, you can refer to the Salesforce Help Documentation: Apex error 'List has no rows for assignment to SObject' .

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

    Salesforce: System.QueryException: List has no rows for assignment to SObject (3 Solutions!!) Roel Van de Paar 149K subscribers Subscribed 4 728 views 3 years ago

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

    System.QueryException: List has no rows for assignment to SObject | Salesforce troubleshooting error SFDC Quest 1.97K subscribers Like 2.8K views 1 year ago Troubleshooting Salesforce Issues ...more

  12. Apex Testing Error: System.QueryException: List has no rows for

    Salesforce has some syntactic sugar that allows us to stick the result of a query into a single SObject instance, but only when the query returns exactly 1 row.

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

  14. Salesforce

    Salesforce - List has no rows for assignment to SObject if you using following code in Apex and Error should throw like "List has no rows for assignment to ...

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

    Solution for "System.QueryException: List has no rows for assignment to SObject" - Null Coalescing Operator (??) | Spring'24 Release by Rahul Malhotra on Monday, January 22, 2024 in Apex , Apex Basics , Salesforce , Salesforce Developers Hello Trailblazers,

  16. apex

    System.QueryException: List has no rows for assignment to SObject - In Test Class Ask Question Asked 5 months ago Modified 5 months ago Viewed 39 times

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

    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. If the ContentVersionId file is not shared, share it manually with the library/user.

  18. List has no rows for assignment to

    I am trying to display a selectList in a visualforce page using a custom controller i built. I get an "List has no rows for assignment to SObject"

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