Skip to main content
26 votes
Accepted

Reverse list in Apex

No such method currently exists on the List class as of Winter '17 (API v38). The only way to do this in Apex that I'm aware of is to loop over the list starting from the end. List<Object> ...
Derek F's user avatar
  • 62.4k
24 votes

List.contains in not working

Since so many people have viewed this question, I wanted to provide an updated answer to this for anybody who visits this question in 2018 & beyond: The upcoming version of Salesforce (Spring '18) ...
Robert Watson's user avatar
19 votes
Accepted

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

I agree with Rahul's comment that the following is probably the best you can do: String payload = '{"data": [{"s": "a", "i": 1}]}'; Map<String, Object> deserialized = (Map<String, Object>)...
Adrian Larson's user avatar
  • 150k
16 votes

List sorting by custom field

You have to implement a Wrapper class with comparable interface for this to happen. To implement a custom sort order for sObjects in lists, create a wrapper class for the sObject and implement ...
Pranay Jaiswal's user avatar
14 votes
Accepted

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

You can't cast ANY to a more specific type, so you have to actually obtain the type that you want to return. Without proper reflection, you have to tell the function what type you'd like to return. ...
sfdcfox's user avatar
  • 495k
14 votes

How to convert List to a Map?

You begin your question with: I have a List<custom_object__c>. I want to convert to a Map, where the ID is the String and the value to be an custom_object__c object. Then you show us code that ...
crmprogdev's user avatar
14 votes
Accepted

Null check OR isEmpty Check

The straight SOQL accounts = [select id, .. from Account ...] always returns a list You can usually avoid even having to test for list empty by coding your methods to accept lists as arguments and ...
cropredy's user avatar
  • 71.9k
14 votes
Accepted

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

Here's how'd I do it: public static SObject[] mergeData(SObject[] list1, SObject[] list2) { Map<Id, SObject> results = new Map<Id, SObject>(); for(SObject[] recordList: new List&...
sfdcfox's user avatar
  • 495k
13 votes
Accepted

Convert a Map Values to a List of values

The map's value (ProductWrapper) is incompatible with the list's value (Product2). You'd have to extract the values manually: for(ProductWrapper wrapper: mapProducts.values()) { productList.add(...
sfdcfox's user avatar
  • 495k
13 votes
Accepted

Collection store exception adding all List<Opportunity> to List<Account>

Instantiate a new generic list and it won't carry over any type information. Simply casting to List<SObject> doesn't remove the fact it was originally a List<Account>. List<SObject> ...
Adrian Larson's user avatar
  • 150k
11 votes

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

You could use JSON serialization to format all the records in the collection. E.g. List<Account> accs = new List<Account>(); for(integer i = 0; i < 50; i++) { accs.add(new ...
Daniel Ballinger's user avatar
11 votes
Accepted

Public Static List Method

Here are the components of that method declaration: Access Modifier - public Static vs. Instance - static Return Type - List<List<SObject>> Method Name - searchContactsAndLeads Parameters ...
Adrian Larson's user avatar
  • 150k
11 votes

Null List VS Empty List

Your assumption is incorrect and leadList1 will actually be an empty list. You would get an error if you tried: Lead singleLead = [SELECT Id FROM Lead LIMIT 1]; The reason such an assignment would ...
Adrian Larson's user avatar
  • 150k
11 votes
Accepted

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

actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce ...
Oleksandr Berehovskyi's user avatar
10 votes

Reverse list in Apex

Just for completeness, you could also implement a custom reverse iterator : public class ReverseIterator implements Iterator<Object> { public List<Object> internalRef; integer ...
IllusiveBrian's user avatar
10 votes
Accepted

Traverse through List<List <Opportunity>> (multi-dimension)

You're close to the solution. The thing that you're getting caught on is that the outer loop isn't iterating over a single Opportunity, but rather a List<Opportunity> // Outer loop on a List&...
Derek F's user avatar
  • 62.4k
10 votes

Apex - break list into multiple small lists before processing

well, you can use the following my class that I have written with help of community to split List into parts. To read about main logic of splitting you can check How to write a generic list method ...
Oleksandr Berehovskyi's user avatar
10 votes

SOQL: Populate a Literal List in WHERE IN Clause

You can supply string variable values using Apex binding. When you're not using Dynamic SOQL, as you're not here, you can even use complex Apex expressions in the bind. The following options are all ...
David Reed's user avatar
  • 93.2k
10 votes
Accepted

How can I query the supported timezones in Apex?

If you call TimeZone.getTimeZone with an invalid SID you get back the "GMT" time zone instance - this is one way to validate that your SID is supported (by checking that the SID for the object you get ...
Phil W's user avatar
  • 36.8k
9 votes
Accepted

Can't populate a List in a loop

Right now, your loop is modifying the same instance of HCPFacility in each iteration of the loop (and then storing that in the list). Lists store references of the objects you place into them (rather ...
Derek F's user avatar
  • 62.4k
9 votes
Accepted

List.indexOf does not exist?

This method was introduced in the Spring '18 Release, which likely hasn't been rolled out to your org yet. You can see the calendar for major releases at https://status.salesforce.com/status. For ...
Adrian Larson's user avatar
  • 150k
8 votes
Accepted

How To Group Records By Value In A Specific Field?

You can use a List as a Value for a Map. Map<Integer, ABC__c[]> codeToABC = new Map<Integer, ABC__c[]>(); for(ABC__c record: abcRecords) { if(codeToABC.containsKey(record.Code__c)) { ...
sfdcfox's user avatar
  • 495k
8 votes

List query results into a string

For this you will need to loop over the multiple records returned by the query. One way to do that is: String[] names = new String[] {}; for (NPD_Account_Team__c t : [ SELECT User__r....
Keith C's user avatar
  • 136k
8 votes
Accepted

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

When you have a Map<T1, T2> and call values, you get a List<T2>. So when you have a Map<T, List<T>>, you get a List<List<T>>. Basically, you're going to have to ...
Adrian Larson's user avatar
  • 150k
8 votes
Accepted

Map behaviour populating with List

When you add a record to a collection (or, in your case, use one collection to build another), what you're doing is making references to the original records. Because they are references, when you ...
Derek F's user avatar
  • 62.4k
8 votes
Accepted

Generic parameter not accepted for sets?

The Set collection type does not allow you to implicitly cast, unlike with List. You can, however, overload the signature to work around the issue. You have to do some tricky maneuvering to genericize ...
Adrian Larson's user avatar
  • 150k
8 votes
Accepted

SOQL: Populate a Literal List in WHERE IN Clause

It's like normal SOQL, but you put the colons before each item in the list: public class MyClass { private final String STRING_ONE = 'STRING ONE'; public MyClass(String stringTwo) { ...
sfdcfox's user avatar
  • 495k
8 votes

How to add many values at once in Apex?

You can initialize list with your items: List<String> l = new List<String>{'a', 'b', 'c', 'etc'};
flameless's user avatar
7 votes
Accepted

Controller object not updating fast enough

A good way to do that is to skip the method and use the basic visualforce setters. <apex:inputCheckbox value="{!myCheckboxValues[0]}"> <apex:actionSupport event="onchange" reRender="...
Fernando Gavinho's user avatar
7 votes

Controller object not updating fast enough

This is primarily due to network latency. When using apex:actionSupport, an ajax call is initiated to the server. These calls are non-blocking, meaning you can initiate several, completely ...
S Morgan Robertson's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible