Salesforce apex list class and methods with practice examples

salesforce apex list

List is one of the collections in apex. A list is an ordered collection of elements, where each element can be accessed by its index. Apex lists class provides a number of methods to handle groups of records in salesforce.

In this guide, we will dive into list manipulation in salesforce apex, explaining essential list methods to use for real-world business scenarios.

  • How to Create a List in Apex

What are the list methods

Exception - list has no rows for assignment to sobject, exception - list index out of bounds, apex list practice examples, how to create a list.

To create a list , use the List keyword followed by angle brackets containing the data type.

Here is an example :

Note : Lists must always be initialized. If you operate on an uninitialized list, you may cause a null pointer exception.

Salesforce apex list class provides a rich set of methods to manipulate lists. These methods allow developers to perform a variety of operations, such as adding, removing, sorting, and searching elements within a list.

The following are some of the most important list class methods available in Salesforce.

This method adds an element to the end of the list.

This method adds all the elements from another list to the end of the current list.

This method removes all elements from the list.

4. contains

This method returns true if the list contains the specified element, otherwise it returns false.

This method returns the element at the specified index in the list.

This method returns the index of the first occurrence of the specified element in the list, or-1 if the element is not found.

This method returns true if the list is empty,otherwise it returns false.

This method removes the first occurrence of the specified element from the list.

This method replaces the element at the specified index in the list with the specified element.

This method returns the number of elements in the list.

This method sorts the elements in the list in ascending order, based on their natural ordering.

This exception happens when you try to use a value from a list that was created using a SOQL query, but the list is empty.

For example:

The select query always returns a list. Always check if the list is empty before accessing the values from the list.

The modified code for the above scenario is :

Apex throws a "List index out of bounds" exception when you try to access an element in a list using an index that doesn't exist in the list. Here is an example:

To avoid this happening in your code, always check the list size before accessing an index.

Following are a list of practice examples to learn and use the apex list methods . Click the following links to see the apex practice problems.

1. Add two string values to a list

Apex practice method to define and add two string values into a list.

2. Find the number of elements in a list

Apex practice method to find the number of elements in a list using apex list methods.

3. Insert an element at a specific index

Apex list practice example to add an element at specific index by using apex list methods.

4. Verify if an opportunity stage is in a list of final stages

Apex list method to verify if the opportunity stage belongs to one of the final stages.

5. Dedupe lead records in salesforce using apex

Learn to use apex list methods to deduplicate lead records in salesforce.

6. Find account ids from list of accounts

Retrieve account ids from any list of accounts using apex list methods.

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

Apex error 'list has no rows for assignment to sobject'.

The following query is not returning any number of records: "[SELECT Id FROM Account WHERE Id = :Trigger.new[0].Account__c]" The error "List has no rows for assignment to SObject" occurs when query doesn't return any rows.

salesforce apex list has no rows for assignment to sobject

List has no rows for assignment to SObject although query returns rows

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" error when trying to preview the page, but running the query in the developer console, returns the rows.

here is my page:

<apex:page Controller="BpmIcountPayment">

<apex:param name="first_name" value="Account.FirstName"/>

<apex:param name="last_name" value="Account.LastName"/>

<apex:param name="id" value="Account.idnumber__c"/>

<apex:param name="address" value="Account.Address"/>

<apex:form >

<apex:selectList value="{!productsTitle}" multiselect="false">

<apex:selectOptions value="{!ProductsLov}"></apex:selectOptions>

</apex:selectList>

</apex:form>

</apex:page>

and my controller:

public class BpmIcountPayment{

private final Account account;

public String productsTitle {

get { return 'products for sale'; }

public BpmIcountPayment() {

account = [SELECT Id, Name, Site FROM Account

WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

public Account getAccount() {

return account;

public List<SelectOption> getProductsLov() {

List<SelectOption> products = new List<SelectOption>();

List<Product2> productsList = [SELECT Id, Name, Family

FROM Product2

WHERE (Family = 'ShopProduct')

OR (Family = 'CourseParent')

OR (Family = 'SFCourseProgram')];

system.debug(productsList);

for (Product2 currProduct : productsList) {

products.add(new SelectOption(currProduct.Id, currProduct.Name));

return products;

  • Nach "Am hilfreichsten" sortieren
  • Nach Datum sortieren

salesforce apex list has no rows for assignment to sobject

preview url : 

https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?core.apexpages.request.devconsole=1

after apex/vfpage_name pass the id of any account like this: 

https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?id=0012800000toEnM

Please try this, if the problem sustain then let me know.

  • Unterstreichen
  • Durchstreichen
  • Liste mit Gliederungspunkten
  • Nummerierte Liste
  • Link hinzufügen
  • Bild einfügen
  • Dateien anhängen

IMAGES

  1. apex

    salesforce apex list has no rows for assignment to sobject

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

    salesforce apex list has no rows for assignment to sobject

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

    salesforce apex list has no rows for assignment to sobject

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

    salesforce apex list has no rows for assignment to sobject

  5. apex

    salesforce apex list has no rows for assignment to sobject

  6. Understanding 'List has no rows for assignment to SObject' in a trigger

    salesforce apex list has no rows for assignment to sobject

VIDEO

  1. Apex

  2. Salesforce Platform dev 1 Apex Introduction 2

  3. #7 List

  4. Apex Basics & Database

  5. Managing and Deleting Duplicate Attachments in Salesforce using LWC and Apex

  6. Use sObjects

COMMENTS

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

  2. apex - List has no rows for assignment to SObject - Test ...

    Assigning the results of a SOQL query to a single SObject rather than a List results in a runtime failure unless there is 1 and only 1 result. You could change it to: List<Account> accounts = [SELECT Id FROM Account]; Or add a where clause or LIMIT clause...

  3. apex - Understanding 'List has no rows for assignment to ...

    The likely reason for your List has no row error is that you are trying to SOQL into a single sObject and not a list and this will throw an error when no rows are returned. In this case, no rows are returned from your Implementation query because of the issue with after update and how you've structured your test.

  4. apex - System.QueryException: List has no rows for assignment ...

    Salesforce throws this error when you assign a query result to a singular instance of an object. If you were assigning to a list (or map) this would not be the case, you would just be given an empty list.

  5. apex - System.QueryException: List has no rows for assignment ...

    System.QueryException: List has no rows for assignment to SObject. Error shown in this line: opportunities = [SELECT Name,Amount,StageName FROM opportunity WHERE Id = :recId AND StageName NOT IN ('Closed Lost', 'Closed Won')];

  6. 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. Løsning While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned.

  7. Salesforce apex list methods with practice examples - Decodeforce

    Exception - List has no rows for assignment to SObject This exception happens when you try to use a value from a list that was created using a SOQL query, but the list is empty. For example:

  8. apex - "List has no rows for assignment to SObject ...

    This may be probably due to the fact that recordId is undefined in connectedCallback () as I can see the value logged in console is undefined. When using headless quick actions, implement invoke () method to read the recordId and then trigger any Apex controller actions. You can find more details here.

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

    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

  10. List has no rows for assignment to | Salesforce Trailblazer ...">List has no rows for assignment to | Salesforce Trailblazer ...

    I get an "List has no rows for assignment to SObject" error when trying to preview the page, but running the query in the developer console, returns the rows. here is my page: <apex:page Controller="BpmIcountPayment">