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.

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.

Matt Lacey's user avatar

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

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

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

Hot Network Questions

list has no rows for assignment to sobject in lwc

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 .

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.

RealGigex's user avatar

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.

eyescream's user avatar

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 .

Hot Network Questions

list has no rows for assignment to sobject in lwc

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 .

list has no rows for assignment to sobject in lwc

list has no rows for assignment to sobject in lwc

Don't have an account?

Browse by Topic

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

Sign in to start a discussion

Laaral

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);                      } }

hitesh90

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

Rosh

 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); } } }

Dmytro Kurylo

You need to sign in to do that.

Select a category.

list has no rows for assignment to sobject in lwc

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];

' src=

Sakthivel Madesh

3x Salesforce MVP | Platform Champion | 20x 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, meet the 2023 salesforce mvp hall of fame..., free salesforce certification days webinars – march 2023, build your chrome extensions using lwc (lwr), kapil batra – salesforce trailblazer talks, winter 22 – salesforce certification maintenance exam deadline..., spring 23 – return characters in a string..., spring 23 – track field history for activities..., free salesforce certification days webinars – december 2022, mohit agarwal – salesforce trailblazer talks, salesforce – free weekend learning from pluralsight –..., leave a comment cancel reply.

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

CAPTCHA

CAPTCHA Code *

We only have what we give

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.

list has no rows for assignment to sobject in lwc

Post navigation

Previous post.

IMAGES

  1. apex

    list has no rows for assignment to sobject in lwc

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

    list has no rows for assignment to sobject in lwc

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

    list has no rows for assignment to sobject in lwc

  4. Salesforce: Testing page for entry

    list has no rows for assignment to sobject in lwc

  5. How to remove the error "List has no rows for assignment to sObject"

    list has no rows for assignment to sobject in lwc

  6. flow error Error Message: core.email.template.TemplateRenderingException: List has no rows for

    list has no rows for assignment to sobject in lwc

VIDEO

  1. Correndo do CAOS com meu mano no FALL GUYS

  2. BOMBA! BOLSONARO SERÁ PRESIDENTE! VAZOU DOCUMENTO SECRETO E PODE SER O FIM DE LULA! CPI SERÁ CRIADA!

  3. We're the Millers FuLLMovie HD (QUALITY)

  4. My Absolute Final BTNL (Part 5)

  5. Apex Legends #apexclips #apexlegendsps #apexmemes #ps #apex #games #pc #apexlegends

  6. YouTube Fix History This list has no videos Problem Solve

COMMENTS

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

  2. 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. - Daniel Ballinger Mar 2, 2015 at 18:33

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

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

    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. Cookies Details‎

  5. salesforce

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

  6. list has no rows for assignment to sobject

    Just try the below code public List distributionIDs {get;set;} distributionIds= [select Id from Distribution_List__c where Business_Partner__c=:relatedAccountId AND Start_Date__c today AND Status__c='Active'].Id; distItemLst= [select id from Distribution_List_Item__c where Distribution_List__c in :distributionIds]; …

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

    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) {

  8. How to remove the error "List has no rows for assignment to sObject

    I have a method in Visualforce Controller where I am saving the query result in a list. Here is my list. listApexJob = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID =: batchProcessId ]; How to remove the error "List has no rows for assignment to sObject"?

  9. 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 SObject". Account a = [Select name from acoount where id =: strId]; Try to avoid the error following code in you Apex.

  10. Salesforce Error: List has no rows for assignment to SObject

    (Note; the list of Objects are in Alpha order, scroll down to the letter "i") 8) Click Edit. 9) Go to the section "Field Permissions." 10) Look for boxes that are unchecked. *(note; ignore boxes that are greyed out, or that cannot be checked.) 11) If you see a box that is unchecked, check both Read Access and Edit Access.

  11. Apex error

    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!

  12. System.Query Exception: List has no rows for assignment to s-object

    System.Query Exception: List has no rows for assignment to s-object? How to resolve it? Avnish Yadav replied 4 years, 5 months ago 4 Members ... Deploying LWC OSS Apps to Heroku With and Without Express API Server | Salesforce Developer Guide. Blog in Others, Salesforce. Heroku is a platform-as-a-service offering that enables developers to ...

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

    Salesforce: List has no rows for assignment to SObject - Test ClassHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks &...