- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

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.
List has no rows for assignment to SObject error although query returns rows
I'm a bit new to apex and I am trying to display a selectList in a visualforce page using a custom controller i built.
I get a "List has no rows for assignment to SObject" error when trying to preview the visualforce page, but running the query in the developer console, returns the rows.
here is my page:
and my controller:
Just to clarify the query i'm referring to is the query in getProductsLov() .
My API version is 40 and i am working in a sandbox environment.
- visualforce

Impossible. If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. This eliminates getProductsLov (unless you didn't post whole code) because there you assign to a list.
Humo(u)r me and System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); in your constructor before firing that query...
You're viewing the page with valid Account Id passed in the URL? And that Account is visible for your current user? If the page is account-specific, try using <apex:page standardController="Account" extensions="BpmIcountPayment">... (you'll have to provide a different constructor in apex first). This could simplify your code a lot.
- You are right! The page displays correctly after adding a valid "id" parameter to the url. Thank you! – RealGigex Feb 7, 2018 at 8:51
Your Answer
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 , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged salesforce visualforce apex soql or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- The [amazon] tag is being burninated
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Checking system vs. SEPA and the like
- "We, who've been connected by blood to Prussia's throne and people since Düppel"
- Egypt - Duty free notation on passport
- Do roots of these polynomials approach the negative of the Euler-Mascheroni constant?
- Finite abelian groups with fewer automorphisms than a subgroup
- The number of distinct words in a sentence
- Should I use the mean or median of my data for queueing models?
- Draw Parcly Taxel's cutie mark
- Are immutable variables part of the contract storage?
- How to handle missing value if imputation doesnt make sense
- If a law is new but its interpretation is vague, can the courts directly ask the drafters the intent and official interpretation of their law?
- Theoretically Correct vs Practical Notation
- What's the difference between a power rail and a signal line?
- Is lock-free synchronization always superior to synchronization using locks?
- Wahoo Kickr with 8 speed cassette?
- How to prove that the supernatural or paranormal doesn't exist?
- Mathematica calculates the parameter value when the first derivative is 0
- Minimising the environmental effects of my dyson brain
- What is the purpose of this D-shaped ring at the base of the tongue on my hiking boots?
- Has 90% of ice around Antarctica disappeared in less than a decade?
- How does fire heat air?
- Book about a good dark lord, think "not Sauron"
- Why doesn't my passport show if a foreign country has banned me?
- Governance of one Solochain by Another using XCM and Trustless Bridge?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Stack Exchange Network
Stack Exchange network consists of 181 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
List has no rows for assignment to SObject in a test class
I need to create a test class for the below mentioned class so far I am getting the error "List has no rows for assignment ".Here customer address is an object whose parent is contact

Since Contact is parent for Customer_Address__c Object, there might be case that one Contact can have multiple Customer_Address__c Object.
So replace this line:
Also in your test class you're not creating customer_address__c data. That's why you're getting that error. Add below lines after inserting contact in your test class:
- I made the required changes but still I am getting the sameerror – Saswata Das May 8, 2017 at 9:10
- @SaswataDas on which line you're getting an error.? – Rohit Mourya May 8, 2017 at 9:11
- I am getting the error in this line WelcomePageController.getCustomerAddressId(); – Saswata Das May 8, 2017 at 9:13
- Are you returning like this: return customerAddressList[0].Customer_Address_ID__c ; because now it would a list . Also since you've made some changes. Post your code. – Rohit Mourya May 8, 2017 at 9:16
- 1 @SaswataDas Before calling getCustomerAddressId() from test class, add System.runAs(user) since you're retrieving current logged in user in your controller. – Rohit Mourya May 10, 2017 at 9:00

Your Answer
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 , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged unit-test or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
Hot Network Questions
- Minimising the environmental effects of my dyson brain
- Can the Spiritual Weapon spell be used as cover?
- Is there any way to orbit around the object instead of a 3D cursor?
- Getting timestamp of each time pings enter and leave a radius
- Does Counterspell prevent from any further spells being cast on a given turn?
- Nought and Crosses (tic-tac-toe) in C++
- If you order a special airline meal (e.g. vegan) just to try it, does this inconvenience the caterers and staff?
- Transgenic Peach DNA Splicing
- Theoretically Correct vs Practical Notation
- Mathematica not solving this integral
- A-Z related to countries
- Short story taking place on a toroidal planet or moon involving flying
- Tips for golfing in SVG
- What is the correct way to screw wall and ceiling drywalls?
- Small bright constellation on the photo
- Are the wavefunctions of electrons in conductors localised?
- How can I make these specific modifications using tcolorbox
- How to prove that the supernatural or paranormal doesn't exist?
- Why doesn't my passport show if a foreign country has banned me?
- Follow Up: struct sockaddr storage initialization by network format-string
- ncdu: What's going on with this second size column?
- Why does hooking voltmeter to two transformers show 0 voltage?
- QGIS - Countif function
- Do you really get double penalized for rolling HSA excess contributions to the next year?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

- My Developer Account >
- Create Account>
- My Settings>
- All Developer Centers
- Community Cloud
- Einstein Analytics
- Einstein Platform
- Embedded Service SDK for Mobile
- Heroku Developer Center
- Integration and APIs
- Lightning Apps
- Lightning Flow
- Marketing Cloud
- Mobile Developer Center
- Mulesoft Developer Center
- Pardot Developer Center
- Quip Developer Center
- Salesforce DX
- Service Cloud
- Lightning Component Library
- Lightning Design System
- Lightning Aura Components
- All Documentation
- Certification
- Sample Gallery
- Video Gallery
- Events and Webinars
- Success Stories
- Developer Groups
- Trailhead Resources
- Leading Through Change with Data
- COVID-19 Data Hub
- COVID-19 Global Daily Tracker
- Global Economy Data Track
- Government Data Track
- Healthcare Data Track
- B-Well Together
- Leading Through Change
- Salesforce Care
- AppExchange Resources
- MuleSoft Resources

Don't have an account?
Browse by Topic
- View More Topics
- See All Posts
Welcome to Support!
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
- All Questions
- Unanswered Questions
- Unsolved Questions
- Solved Questions
- Date Posted
- Recent Activity
- Most Popular
Sign in to start a discussion
- This Question

System.QueryException: List has no rows for assignment to SObject
Hi , I'm getting this error message : System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SetupChangeTechnologyListCheck: execution of AfterInsert caused by System.QueryException: List has no rows for assignment to SObject: : line 8, column 1
I don't understand why it doesn't find account objects, because there are alot of them in SF ? Where should I start looking for an answer to this? Many thanks already, if you could somehow help me understand this better.
trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update) { for(Setup__c s : trigger.new) { Account acc = [SELECT Name, Service_Type_List__c FROM Account WHERE RecordType.Name = 'Main Account' AND Name = :s.Main_Account__c]; UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList(); techList.UpdateList(acc); } }
- December 13, 2013
The exception is because of you are not getting any record in your SOQL query..
which meet the where criteria so it's give error.
Important : Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits. Thank You, Hitesh Patel SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant My Blog:- http://mrjavascript.blogspot.in/
All Answers
Its always a best practice to check if list is not empty before doing any DML operation.
Your code could be modified tso that it doen't throw an exception:
trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update) {
for(Setup__c s : trigger.new) { list<Account> accountList = [SELECT Name, Service_Type_List__c FROM Account WHERE RecordType.Name = 'Main Account' AND Name = :s.Main_Account__c]; Account acc = (accountList != null && accountList.size()>1) ? (accountList[0] : null; if (acc != null) { UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList(); techList.UpdateList(acc); } } }

- April 25, 2014
You need to sign in to do that.
Select a category.

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 Mar 28, 2016 at 20:42
The list (the result of your select) is not returning any rows, which means the assignment to your SObject m is failing. Make sure that the query is correct. Is the parameter you use in the query being set on the page? Is there data that fulfils the criteria? Also consider adding some Apex Pagemessage error elements. Share Improve this answer
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!
If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. This eliminates getProductsLov (unless you didn't post whole code) because there you assign to a list. Humo (u)r me and System.debug (JSON.serializePretty (ApexPages.currentPage ().getParameters ())); in your constructor before firing that query...
1 Answer. Sorted by: 1. Since Contact is parent for Customer_Address__c Object, there might be case that one Contact can have multiple Customer_Address__c Object. So replace this line: Customer_Address__c customerAddress= [SELECT ID,Customer__c,Customer_Address_ID__c FROM Customer_Address__c WHERE Customer__c =:contact.Id ]; With: List<Customer ...
First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SetupChangeTechnologyListCheck: execution of AfterInsert caused by System.QueryException: List has no rows for assignment to SObject: : line 8, column 1 I don't understand why it doesn't find account objects, because there are alot of them in SF ?
Resolution 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 populated by a workflow rule. This issue can also occur when the user attempts to reconfigure a Primary Quote without Read access to the associated Opportunity.
Salesforce: List has no rows for assignment to SObject - Test ClassHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks &...