27 votes
Accepted

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

The following methodology may be somewhat faulty but demonstrates a large difference. The get approach is more than 4 times slower. Square Brackets List<Integer> numbers = new List<Integer&...
Adrian Larson's user avatar
  • 150k
25 votes
Accepted

List.contains() method in Salesforce

Edit: This is now a function you can use. It was implemented in the Spring 18 release. I don't know of an official reason "why," but there's an idea you can vote on. I'll be sure to ask ...
sfdcfox's user avatar
  • 487k
21 votes

Convert a Set<Id> to Set<String> using Apex

You can explicitly cast from List<Id> to List<String> and vice-versa and use that to convert Sets between types by converting the Set to a List first and passing the List a new Set<>( ...
E.J. Wilburn's user avatar
  • 3,797
19 votes
Accepted

Is there a specific reason why we can't upcast Sets?

After doing a deeper dive with google, I think I have the answer. It turns out that this is a behavior defined by Java to ensure type safety. In Java, a Set<String> is a 'Generic' collection. ...
Derek F's user avatar
  • 60.9k
17 votes

Why Set<Sobject> doesn't support DML operation

One reason may be that Set<SObject> is a risky mechanism to use: equality is based on all the fields (so is expensive) and if fields are changed logic can easily break. Same problem using ...
Keith C's user avatar
  • 136k
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
  • 487k
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
  • 487k
13 votes

List.contains() method in Salesforce

You can now use List.contains() with Spring 18
Anand13 S's user avatar
  • 151
9 votes
Accepted

How to choose using Collection<Id> rather than Collection<String>, or the opposite?

Both String and Id are primitive data types in apex. Consider below scenario: public class poc { static List<Id> ids = new List<Id>(); public static void check() { try { ...
salesforce-sas's user avatar
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

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

How to Compare two sets namely set A and set B while both contains set of ids then I need to remove id from second set

You can use "containsAll" and "removeAll" method of set. list: Boolean containsAll = satb.containsAll(setA); This "ContainsAll" method will store true if all the elements are present in setB. ...
Nitin's user avatar
  • 1,475
7 votes
Accepted

any shortcut to get set<String> from map<Id,Set<String>> ?

You have to loop through and use addAll. There is no shortcut. Set<String> allValues = new Set<String>(); for (Set<String> subset : st.values()) { allValues.addAll(subset); }
Adrian Larson's user avatar
  • 150k
7 votes

What exactly does the 'Base Collect Tracking Code' in Personalization Builder refer to?

The Base collect code is just the collect.js, this script does not collect any information. All information that is tracked is gather by the track calls. There is also a Known Issue for this behavior
EazyE's user avatar
  • 8,449
6 votes
Accepted

Apex equivalent to associative array?

You need to create a Wrapper class for this. Example : public class TestWrapper { public static void pushValue(){ list<fieldWrapper> n = new list<fieldWrapper>(); ...
vignesh's user avatar
  • 2,704
5 votes
Accepted

iterator() on a set. Reason for it not being in the Apex Doc?

Set does have an iterator method; this is used internally by the for-in loop syntax. However, it does not implement the Iterable interface. This notably means that new methods, like String.join, do ...
sfdcfox's user avatar
  • 487k
5 votes

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

I don't know if Salesforce changed its algorithms, but I just tested it, both in my Sandbox and Production, and it turns out the method List.get() is almost always faster than square brackets [i]. My ...
Matheus Gonçalves's user avatar
5 votes
Accepted

Compile Error: Loop must iterate over a collection type: Map<Id,List<Event>>

Since you have declared Map<Id, List<Event>> events = new Map<Id, List<Event>>(); So, to loop through the List of events you need to use events.values() for (List<Event>...
Santanu Boral's user avatar
5 votes
Accepted

Pass Case Ids collection to visual workflow from Case List View Custom Button and update cases taking from input screen

Create a List View Custom button and use this code to pass parameters to the flow. Flow parameters: vSelectedCaseIds which contains case ids collection which can be found from {!GETRECORDIDS($...
Santanu Boral's user avatar
5 votes

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

Looks like Set does not implement iterable. Code: System.debug(new List<Object>() instanceof Iterable<Object>); Output: Compile failure on line 2, column 14: Operation instanceof is ...
Vladyslav K's user avatar
  • 2,733
5 votes
Accepted

What is the Correct collection class structure to hold SOQL subquery results?

You don't need any helper classes at all. You can just iterate through the results and update the parents accordingly. In this case the "correct" structure is the one you find easiest to write and ...
Adrian Larson's user avatar
  • 150k
5 votes

Why Set<Sobject> doesn't support DML operation

Sets (and Maps) cannot be cast to Sets of a different type. For example, Set<sObject> t = (Set<sObject>)new Set<Contact>() will fail with the message Incompatible types since an ...
IllusiveBrian's user avatar
5 votes
Accepted

Facing "common.apex.runtime.impl.ScalarList@a1324f1f"

cont.Discipline_Description__c = String.join(contactDisciplineMap.values(), ' '); You're trying to join the List<List<String>>, which calls the default toString method, which isn't what ...
sfdcfox's user avatar
  • 487k
5 votes
Accepted

How to sort a list of list

You can implement Comparable interface for your custom class and determine your rule in compareTo method. public class ListItem implements Comparable { public Integer first {get; set;} public ...
Oleksandr Berehovskyi's user avatar
4 votes
Accepted

SOQL For Loop Architecture Question

You get a significant heap benefit by using the former. It induces batching (see SOQL For Loops) so it doesn't have to load the whole list into memory at once. Money quote (emphasis mine): SOQL for ...
Adrian Larson's user avatar
  • 150k
4 votes
Accepted

When should I use Maps and when should I use Lists in Apex?

Personally I always try to use Maps when working with an update trigger. Especially when I need to check if some field has changed value. Using maps I can loop through all old and new values in one ...
Novarg's user avatar
  • 5,256
4 votes

When should I use Maps and when should I use Lists in Apex?

I guess this question boils down to "When do I use Maps, Sets and Lists". Whether you're using an Apex Trigger or just a process within an Apex class their usage is the same regardless. This question ...
Dan Jones's user avatar
  • 7,167
4 votes

List<string> how much values/Size can it store?

A list can store many thousands of items and the total space used by the items and list can be several megabytes. But to convert a list into a string that represents all the items in the list, you ...
Keith C's user avatar
  • 136k
4 votes

Get Unique keys map and Repeated keys map

This is not possible, maps do always have unique keys! If you need multiple values for one key, you could save them in a list and add it to a map with the following structure: List<String> ...
itsmebasti's user avatar
  • 6,399
4 votes
Accepted

Exception not being caught; System.FinalException: Cannot modify a collection while it is being iterated

Your exception is not being caught because FinalException is not catchable. Neither are LimitException nor AssertException. You can quickly check for yourself if a given type of exception is ...
Adrian Larson's user avatar
  • 150k

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