Questions tagged [list]

A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

Filter by
Sorted by
Tagged with
34
votes
2answers
6k views

When To Use Set vs. List In SOQL

I have always assumed that it is better to use a Set in my query filters instead of a List. For example: Set<Id> parentIds = generateParentIds(); List<Child__c> children = [SELECT Id FROM ...
32
votes
5answers
71k views

list.IsEmpty() vs list.Size() > 0 vs list != null

I want to check the list is empty or not if not proceed further. So, which is preferable in apex and what are the differences between them? if(myList.size() > 0) or if(!myList.isEmpty()) or ...
15
votes
3answers
30k views

Best way to remove element from list or why they kept iterator without this option

I'm studing apex now and dont understand the situation with list and iterator. In java you cant delete in any way elements from list while iterating it. So therefore developers added iterator and also ...
14
votes
2answers
7k views

Performance: List.get(index) vs List[index]

I want to know which approach is better in Salesforce when getting element from list. Is this faster? myList.get(0); Or is this faster? myList[0]; Using large list which above statement is more ...
11
votes
4answers
6k views

How to detect first or last iteration of apex:repeat

I currently have some VisualForce which uses an <apex:repeat/> element and inserts commas in between the items like so: <apex:variable var="idx" value="{!0}"/> <apex:repeat value="{!...
11
votes
2answers
5k views

How to write a generic list<t> method that will cast at runtime?

I recently tripped across the need to take a list of objects (instances of a class--not sobjects) and split them into smaller lists of 200 members each. The unit test below shows the code inside a ...
11
votes
4answers
4k views

Best Approach/Way to Query Single SObject record

To query a single record we have below two approach : Approach 1. Assign record to a single Account variable as shown below: Account accountToGet = [SELECT Name From Account where name = '...
11
votes
3answers
2k views

Lightning: dynamic index in aura attribute with type List

related post I am having the same issue. Trying to access array with dynamic index arrayList[index] (index from aura:iteration), but Salesforce doesn't support that. Does anyone know how to do this?...
10
votes
4answers
6k views

Reverse list in Apex

Can you reverse a list of objects in Apex? In Java and C#, there is a method you can call to reverse a list but I don't see one for Apex...
9
votes
3answers
3k views

Is it possible to Upsert a List<SObject> with external ID specified

I'm getting an error when attempting to compile my Apex: Upsert requires a concrete SObject type In my code, I'm unable to create a concrete declaration for the container which I want to upsert. ...
8
votes
1answer
15k views

Convert a Map Values to a List of values

How to convert Map to List, here is what I have tried but getting the error: List<Product2> productList = new List<Product2>(); productList.addAll(mapProducts.values()); Error: > ...
8
votes
1answer
101 views

How can I safely cast a List<sObject> to a specific concrete type?

I have a List<sObject> genericList in scope in a method. The list was created and populated elsewhere and passed in as a parameter. It could be: a List<sObject> that is homogeneous. E.g. ...
8
votes
5answers
28k views

(Lightning) How do I get the selected value from a Select dropdown list & send it to a method?

I'm trying to implement a single option select dropdown list for my component, and I've gotten as far as displaying the data members and the correct values for them. However, I can't find any example ...
7
votes
2answers
263 views

SOQL: Populate a Literal List in WHERE IN Clause

I need to query for an object based on 2 string variables. The first one is a constant but the second one gets passed to the class constructor. So in the end I want to achieve something like this: ...
7
votes
3answers
9k views

Null check OR isEmpty Check

I have checked many posts but still not clear. A SOQL query returns a list but if SOQL finds no records then it will return an empty list or it will be null? When should i go for isEmpty check or ...
7
votes
2answers
561 views

Lightning List Builder - Need Working Example

I am trying to build a lightning component that includes a list builder. The example supplied does not work out of the box as it uses the depreciated svg component. There is no mention of the list ...
7
votes
1answer
3k views

Assigning VF component attribute to a List controller class variable

I have a VF page using a standard controller, a component, and a custom controller for the component. I am trying to pass a list of sObjects from the standard controller to the component's custom ...
6
votes
4answers
27k views

List.contains in not working

I have to get subset of users who do not have records in custom table last modified in last 15 minutes. Here is my code: string query = 'Select Id FROM MyCustomObject__c where LastModifiedDate > ' ...
6
votes
3answers
10k views

Can I remove element from a list being looped through

In apex, how can I remove an element from a list being looped? List<String> companyinfos=new List<String>(); companyinfos.add('a1'); companyinfos.add('a2'); companyinfos.add('a3'); ...
6
votes
2answers
348 views

What is the difference between using the LIMIT clause and using a fixed index to retrieve data from a query?

This code recover the exact same account, I thought there could be differences with the way it ask the database ... Seems not to be the way it works, So an idea ? public static void ...
6
votes
3answers
7k views

How can I add a new value to a picklist using APEX?

I have queries pulling all of the information correctly but what I can't seem to find was a way to add new options to a picklist using APEX. I currently have Package__c.Products__c.add(new Schema....
6
votes
1answer
495 views

How can I query the supported timezones in Apex?

Apex provides a TimeZone API. You can use this to query the detail of a time zone, named using its "SID" (a value like "Europe/London" or "America/New_York" for example). However, this API doesn't ...
6
votes
1answer
218 views

Doesn't Set<T> collection implement Iterable<T> interface? [duplicate]

I am trying to join String elements of Set with semicolon. Set<String> elements = new Set<String>{ 'foo', 'bar' }; System.debug(String.join(elements, ';')); getting the ...
6
votes
2answers
60 views

Performing function on deselection of inputcheckbox

I have an issue with my check box, I want to perform a function on checking my check box and after deselecting it should come back to its original state. VF Page <apex:outputLabel for="check2" ...
5
votes
4answers
4k views

Better way to use Map of List

I have a map of Id to List of Sobject. To update the map i use below code snippet for(SObject so : listSObject) { if(mapSO.get(so.ParentId) != NULL) mapSO.get(so.PartentId).add(so); ...
5
votes
3answers
1k views

Dynamic Apex Question

I am trying to extend a apex metadata coding example for retrieving the record types for all objects in my org. I know this is basic but I am working my way through coding examples for my 501 ...
5
votes
2answers
298 views

Controller object not updating fast enough

I have a list of booleans, and some checkboxes. When a checkbox is clicked, a corresponding index in the boolean list is changed to true/false. My issue is that if the checkboxes are clicked too fast, ...
5
votes
3answers
9k views

How to print ALL the elements of a List<>?

I would need to print all the elements of a list so to be able to copy them (CTRL+C) . In Debug Log , I can see only the first 10, or something similar . (a temp solution I'm using is to store them ...
5
votes
1answer
1k views

Public Static List Method

I have been studying Salesforce Dev for the past few weeks and can't find any documentation on something very simple. Can anyone explain why I would make the public static method a type of list rather ...
5
votes
1answer
11k views

Error: Invalid conversion from runtime type List<ANY> to List<Map<String,ANY>>

This seems similar to other "Can not cast List to List" questions, except in the other cases the culprit tended to be custom sObjects, whereas nothing like that is relevant here. Why is this cast ...
5
votes
1answer
999 views

User input to an array in Visualforce

I have a Visualforce page that collects user input for 125 integer variables that are then used by the controller to create records. I'd rather use an array instead of defining all those variables, ...
5
votes
1answer
483 views

Advice on a trigger that wont work

My colleagues need to create a number of quotations on an opportunity, send them out and then when the customer comes back with their decided quote, the quote needs to be synced to the opportunity ...
4
votes
3answers
5k views

List query results into a string

I have a requirement that I need to display all Account Team Members for a specific Account in a text field for reporting. using this post as a guide, I can a list of the results but I cannot figure ...
4
votes
2answers
274 views

Adding to a List changes all previous additions

I've been working as a developer for about 3 months, so my terminology may be off. I'm trying to add records to a custom object. As the new record information is added to a List, the previous ...
4
votes
2answers
190 views

how do I call this method with list as an argument from another class?

Below is the name of my method and argument of that method private static void Callout(List<Id>cnt) I am calling above method using following code classname.Callout(cnt.id). //where cnt is ...
4
votes
1answer
771 views

How to efficiently combine 2 Lists<Contact> if the same Contact is in both

I have 2 lists of Contacts, which may both contain the same Contact(s) or they may not. I have a method that combines the lists, meaning if a Contact is in both lists it will take the Contact's ...
4
votes
1answer
349 views

Generic parameter not accepted for sets?

While working on some code for a utility method to remove values from a List, I wrote this method signature, which accepts a List<Object>: public static List<sObject> Exclude(List<...
4
votes
1answer
200 views

Attempt to de-reference a null object: list initialization

I initialize the list at the beginning of the code. In one of the methods, I add values ​​to it. In another method, the list is already empty. But I need these values ​​in the second method. How to ...
4
votes
2answers
1k views

Creating a list of unique value pairs

I have two fields in a table Date_Time__c & Activity__r.ExternalAID__c. I am trying to create a list of unique value pairs of these fields in apex. How can i go about doing this. I am not able to ...
4
votes
1answer
405 views

Possible to include getSObjectType() in for loop parameters?

I'm wanting to build a for loop that can dynamically handle lists passed into a function for it. Something along the lines of the following: public static List<AggregateResult> aggregatePrep(...
4
votes
1answer
4k views

How to sort keys in MAP while grouping

I am trying to insert list of wrapper class records in a MAP taking group name as KEY and related Wrapper class records as value pair . the wrapper classes were public class SCQLWrapperClass { ...
4
votes
1answer
907 views

Visualforce list using Apex function and parameter

So at the moment I have a VisualForce Page that uses the following code: <apex:page standardController="Contact" extensions="RelationshipTreeController"> <apex:repeat value="{!...
4
votes
1answer
149 views

Deserializing a list of jsons

I am trying to deserialize a list of jsons. If I have only one json like so '{"apple":"green"}' I can parse it using JSON.deserializeUntyped: Map<String, Object> input = (Map<String, ...
4
votes
1answer
4k views

DML requires SObject or SObject list type: List<List<Commission_Line__c>>

How do I insert the values from the map? I want to insert the grandchildren records in variable Commlinesmap, but I get this error: DML requires SObject or SObject list type: List> Error is at ...
4
votes
1answer
1k views

List vs Map iterations

In this list to list code where you find Id's on a query and insert the Id's into another pre-existing list worse, better, or same as the map to map code below it? I am trying to use maps to be easier ...
4
votes
4answers
2k views

Trying to get a SOQL query with no duplicates

EDIT: Is there a way where I could just loop through my list and remove the duplicates? It should be easy but I've been having trouble with it. I have tried a bunch of different methods of getting ...
4
votes
1answer
164 views

Visualforce: Can't call sort method on List of Comparable objects

I have a list of custom objects whose class "implements Comparable". In my visualforce page I have {!thelist.sort} as an action for a command link. I get an error: action="#{thelist.sort}": Unknown ...
4
votes
1answer
100 views

Why Salesforce REST API List View Describe Method returns extra values in columns list?

When I try to execute this request GET /vXX.X/sobjects/{sobjectType}/listviews/{queryLocator}/describe which is described in documentation https://developer.salesforce.com/docs/atlas.en-us.api_rest....
4
votes
1answer
302 views

Setter not running on List.add()

Calling the add() method on a list doesn't force the setter to be called. First off, why is the setter not running? Secondly, is there a way to somehow override List.add() and steal the action, or do ...
4
votes
2answers
264 views

Got problem on SelectOptions on Multiple Products

I got products. public List<ProntoEVents_Product__c> getProducts() { if(products == null) { products = [SELECT Id, Name, ProntoEvents_Product_Name__c, End_Date__c, Price__c, ...