Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [collection]

The tag has no usage guidance.

21
votes
4answers
9k views

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

I have a Set<Id> which I want to convert to a Set<String>. I don't want to use a for loop to iterate over it. What is the simplest way to do this?
18
votes
1answer
397 views

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

As most stories here begin, I was writing some code for a project that I was working on, and stumbled across an oddity when writing some unit tests. I was testing a method that has a Set<sObject>...
17
votes
3answers
3k views

Do Apex collections implement iterable?

Do Apex collection classes implement Iterable interface? I want to write a generic function that can take any of the collection classes that support foreach. Is this possible?
17
votes
4answers
3k views

Critical update: “Predictable Iteration Order for Apex Unordered Collections” documentation

Today in one of my sandboxes, I was overjoyed to see the following critical update: "Predictable Iteration Order for Apex Unordered Collections" - The iteration order of unordered collections (maps ...
15
votes
2answers
10k views

List.contains() method in Salesforce

Is there any specific reason why salesforce did not provide list.contains() method. Edit 1: The below methods are added to List class and are available after Spring 18 release to Sandboxes and ...
14
votes
2answers
7k views

Map<ID, Map<string, Map<ID, List<PricebookEntry>>>> Good practice or bad?

I have a question about APEX programming practices. My problem: I'm moving a SOQL query out of a for loop in an algorithm to allow for more processing per execution context. The SOQL queried ...
14
votes
2answers
6k 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 ...
13
votes
2answers
17k views

Inline list of object initialisation

You define a list variable and initialize it at the same time as shown in the following example List<String> myStrings = new List<String> { 'one', 'two' }; How would I do this for a ...
12
votes
2answers
5k views

Can a collection be passed to a method by reference?

If I have a list that I pass into a method to be populated, can I then reference that list variable in the class/method that did the passing, without having to return the list? Here's an example, ...
11
votes
2answers
4k 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 ...
10
votes
3answers
1k views

Why Set<Sobject> doesn't support DML operation

As we know that set<Sobject> and list<Sobject> are collection type. So basic question I do have why Set doesn't support DML operation ? We can perform DML on List<Sobject> but why ...
9
votes
2answers
11k views

How to store and JSON-serialize Map of Objects AND SObjects

This question is an extension to Write a generic JSON-serializable Parameters class without hitting “Apex Type unsupported in JSON: Object” where I learned how to serialize a Map to and from JSON. My ...
7
votes
4answers
729 views

BestPractice: Defining and populating Collections

What is the best practice when using collections. Example: This snippet: shortStudentMap = new Map<string,string>(); shortStudentMap.put('1', 'Student1'); shortStudentMap.put('2', 'Student2');...
7
votes
1answer
13k 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: > ...
7
votes
1answer
120 views

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

Although not documented as a set method, iterator() seems to work just fine on sets: set<String> strings = new set<String> {'a','b'}; system.debug(LoggingLevel.INFO,'next() on set ='+...
6
votes
2answers
3k views

Used jQuery datatable for pagination and got this issue: Collection size 1,378 exceeds maximum size of 1,000

I have implemented client side pagination using jQuery datatable for pagination and got this issue: Collection size 1,378 exceeds maximum size of 1,000. This resultant list of records is from a ...
6
votes
1answer
1k views

Apex Class as Map Keys and Sets

I went through the Salesforce documentation for Using Custom Types in Map Keys and Sets . The example in the documentation is not clear for me. Can someone provide use cases of using apex class as ...
6
votes
1answer
201 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
1answer
1k views

Can the Platform-cache store data with any data type?

I know there are limitations to platform cache though, within the limitation(capacity) is it allowed to store any type of data (Collections, Integer, DateTime, sObject etc...) in the cache? What if ...
5
votes
2answers
9k views

How to iterate over query results containing a sub-collection of results

I've added a custom object named Organisation_Stat which is a child of Account. An Account may have zero or more related Organisation_Stat objects. I want to enumerate Accounts which have related Org ...
5
votes
1answer
1k views

include component seems to be having issues with merge-field syntax inside of a repeat component

I am trying to place an apex:include inside of an apex:repeat component. But I am getting a compile error: <apex:repeat value="{!placeholders}" var="placeholder"> {!placeholder} - {!...
4
votes
1answer
292 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
391 views

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

Requirement From Case list View, user will choose one or more cases and will click on "Update Due Date" button, where it will show a screen to choose the "Due Date". Due Date screen And finally ...
4
votes
2answers
287 views

SOQL For Loop Architecture Question

I have a pretty simple/general question, out of the two approaches below, what is the best method when querying and looping over a collection? Approach 1: for(Account a : [SELECT Id, Name FROM ...
3
votes
2answers
4k views

Collection of List<sObjects> keyed by sObject field

Is it possible to have a Map (or other collection) of sObjects list, keyed by one lookup field of the same sObject. Something like Maps of sObjects (below code doesn't works): Map<Id, List<...
3
votes
1answer
33 views

Update two records of one type when a single record is triggered

I have two opportunities. When I update a single opportunity, based on that I have to pull one more opportunity based on some criteria and update both of them. But as I'm updating through trigger, I'm ...
2
votes
3answers
941 views

Comparing Two Lists of Related Objects

I have two custom objects, Question__c and Response__c. There is a lookup field named Related_Question__c on the Response__c object to relate the two. In my VF controller I have two Lists, one for ...
2
votes
2answers
118 views

Retrieving results from a map

I'm trying to work with and understand maps. I have the following map and I don't know how to retrieve one of the results from it, can some one explain what I'm doing wrong? Map<String, ...
2
votes
1answer
66 views

Accessing and passing Parameters from a SOQL to a Set

I'm trying to find out how can I pass the Attachment ParentId value that is part of a SOQL to a Set of Ids. Just testing things around. Lets say I have the Set ready: Set<Id> eveIds = new ...
2
votes
1answer
261 views

Flow and InvocableMethod Simple Question

How would I create an array of the values from the flow and pass them into the APEX @InvocableMethod? My method is taking in a list of Strings: @InvocableMethod public static List<String> ...
2
votes
4answers
576 views

Apex equivalent to associative array?

I have the following list definition in my Apex controller: List<Object> mylist = new List<Object>(); And I would like to do something like: mylist.add({ key : objKey, label : ...
2
votes
2answers
7k views

How to check the map key value with field value?

I am trying to write condition in the class if "field1__c is not equal to map1 key value and field2__c is not equal to map1 key value " then it should throw an error. After assigning when I checked ...
2
votes
1answer
84 views

Compare 2 collections in salesforce

Trying to compare 2 collections, list and list and for all the cases inserted that match any rule, create a record in other object "caseRulesMatched". Case has 3 custom fields f1, f2, f3 and while ...
2
votes
1answer
1k views

Avoid nested for loop while iterating Map<Integer, Set<Integer>>

I want to get a List<Integer> out of Map<Integer, Set<Integer>>. Map<Integer, Set<Integer>> MAPAgreementAdhoc = new Map<Integer, Set<Integer>>{1 => new ...
2
votes
3answers
194 views

How to Create a Read-Only Collection? [closed]

I want to create a collection (List or Set) which can behave like Trigger.New context. Means If we use the collection out side of it's class, we should not be able to add any element in this ...
2
votes
1answer
133 views

Is there a better way to order my collection?

I'm trying to display a chronological list of CaseHistory records to display case edits. I have the following code: public Map<CaseHistory,String> getHelpdeskHistory() { List<...
2
votes
1answer
1k views

Visual Workflow - Duplicate IDs in Collection Variable

My flow is failing to trigger due to this error: "Sandbox Encountered unhandled fault when running process Data_com_Get_Contacts_Constraint/301J00000000idC exception by user/organization: ...
2
votes
1answer
549 views

WSDL: getting the records from a child List<object> of a parent list in .NET

Is there a way to output a specific value in an list object of a list? I tried this but getting the range just returned int, so I can't actually returned an object. List<SaveResult> ...
2
votes
0answers
992 views

Removing duplicates from a SObject Collection in Visual Flows

I am trying to tackle creating multi-touch campaign attribution using visual workflows and am running into a particular use case. To look at all of the Campaigns that are related to the Opportunity, ...
2
votes
0answers
54 views

Lightning collection databinding is broken on Summer 16 [duplicate]

In Spring 16 I have used controls binded to collection values, and it's been working flawlessly until recent release <aura:component > <aura:handler name="init" value="{!this}" action="{!c....
1
vote
2answers
856 views

Get Unique keys map and Repeated keys map

Trying to form a two maps. One should contain only unique key and its values and another should have repeated keys and its values. Name Position User1 Developer User2 Designer User3 ...
1
vote
2answers
162 views

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

Why is this Exception not being caught? I read that System.LimitException is not caught but didn't see anywhere about System.FinalException. Non-Working Code @AuraEnabled public static List<...
1
vote
2answers
4k views

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

Is it possible to Compare two sets namely set A and set B while both contains set of ids. I need to check if set A contains ids which is present in set B. If it is present then i need to remove ...
1
vote
2answers
175 views

What is the best approach (shorthand form) to iterate a collection of List with varying size?

I have a collection of list which is, List<List<String>> Now, i am iterating it as follows, for(List<List<String>> n: mapSAP.values()) { for(List<...
1
vote
1answer
380 views

Facing “common.apex.runtime.impl.ScalarList@a1324f1f”

I have a child object to contact called"ObjectA".I have a discipline field(text) in the child object and I have the same field in Contact too.Now If I insert 4 child records with different contact ids,...
1
vote
1answer
3k views

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

In the code below, I get an error on the bolded line. Says Loop must iterate over a collection type: Map> Any idea how to troubleshoot? Map<Id, List<Event>> events = new Map<Id, List&...
1
vote
1answer
427 views

how to get value from map when wrapper class is the key

I have a wrapper class of picklist values and labels. I am creating a map with picklistValues and List as key and value types. Now, I am getting an error won this line -- objResults.get(...
1
vote
1answer
183 views

How to compare values in Excel file before uploading?

Inserting excel file using apex, I have 4 columns in file,Column-1 (Name) can contain duplicate values. I want to perform check if column-1 has same values (for example A) and column-2(codes) has ...
1
vote
2answers
4k views

How to fill a map with the object field as key from the query? [duplicate]

I'm doing a query to an sObject with Dynamic apex and I would like to store al keys and values in a map where the map contains the object field as key and the value of that field from my query looking ...
1
vote
2answers
71 views

Difference between declaring a List statement

I am curious to know the difference between these two List statements one using curly braces and other one parenthesis. Any thoughts please. Thanks. List<BPSyncConfiguration__c> bps = new List&...