Questions tagged [map]

A map is a collection of key-value pairs where each unique key maps to a single value.

Filter by
Sorted by
Tagged with
13
votes
1answer
36k views

How to deserialize a JSON String to Apex

How can I deserialize this json object: { "response": { "count": 1, "benchmark": 0.22567009925842, "requests": [ { "request": { ...
24
votes
3answers
860 views

What is a use case where one would use an SObject as a Map key?

In Dan Appleman's book Advanced Apex Programming, he explains how using SObjects as Map keys (i.e. Map<SObject, *value*>) is supported but it's poor practice, since the key is actually ...
20
votes
2answers
554 views

hashCode() is never called when adding to Maps and Sets

Given the following Apex class, which implements equals() and hashCode() as per the Using Custom Types in Map Keys and Sets documentation: public class Foo { private Integer x; public Foo(...
22
votes
2answers
6k views

Using an sObject as a Map key

Are sObjects supported as the keys for Maps? I thought they were, but it appears that any subsequent changes to the key sObject break the mapping to the value. This might be better shown with an ...
10
votes
1answer
21k views

How to handle Maps on Visualforce page

Can anyone please help me to understand the below code. I am not able to understand how we are extracting value from map on visualforce page. Actually i am trying to get the size of value list which ...
11
votes
2answers
783 views

Case-Insensitive Maps?

How is it that describes seem to return case-insensitive maps? For instance, if I create a new custom setting, the following succeeds: Map<String, SObjectField> fields1 = SObjectType....
19
votes
1answer
7k views

Apex Map using “Name” as key

I'm trying create a Map using a name field as key. Sample code: Set<String> priceBooksNames = new Set<String>(); priceBooksNames.add('LN-200'); priceBooksNames.add('LN-100'); Map<...
99
votes
2answers
78k views

Is it possible to run a SOQL Query and get a MAP returned?

It is possible to run query and the results inserted into an Apex list: List<Opportunity> opportunities = [SELECT Opportunity.OwnerId, Opportunity....
8
votes
1answer
26k views

How to iterate over map in Salesforce Lightning Component? [duplicate]

I have a aura map attribute defined in my component file as below < aura:attribute name="FilesNameMap" type="Map" /> But i am not able to understand how to iterate this in the component file ...
18
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 ...
6
votes
1answer
464 views

Native JS Map prototype is overwritten in Visualforce page w/ a header

I've been exploring implementing Angular 2.0 in Salesforce custom pages and found that if I set showHeader=true in the VF page, I run into some issues. It looks like including the header loads "sfdc/...
1
vote
3answers
1k views

How To Group Records By Value In A Specific Field?

I have a List of records of type abc__c. abc__c is a custom object. The record has a field called code__c. What I am trying to accompolish is to group the records in the list based on the code__c. ...
5
votes
2answers
521 views

Decimals pretending to be Integers when used in Maps

I came across some very strange behaviour when working with a Map with Decimal keys today. It seems like the Decimal type can't handle Integer assignments properly, but allows them nonetheless. I've ...
1
vote
3answers
75 views

Quickest way to get a Map<Name, Id> from SOQL ? i.e. Required field to go before object id

Need this map to populate the lookup field on a child object, finding id of lookup object by it's Name. e.g. [SELECT Name, Id FROM Account] --> Map<Name, Id>
13
votes
3answers
8k views

'Map key null not found in map' when using apex:pageBlockTable

I have a simple controller extension with has a method which returns a Map<String, Map<String, Boolean>. public class UserExtension { private final User theUser; public ...
7
votes
1answer
4k views

How to sort Map by values

I need to loop through Accounts and then on all of the Contacts for those Accounts. I need to find what is the address with the most occurrences in Contacts for each Account and update the Account ...
3
votes
2answers
1k views

create a map with keys = fields names and values = query result

I am trying to create a map from a result of a SOQL query, this map contains the name of the fields as keys and the values of the query as values for example: List accounts = [SELECT Name, Email ...
10
votes
2answers
10k views

How to iterate a Map in Aura

I have a aura map attribute defined in my component file as below <aura:attribute name="FilesNameMap" type="Map"> But i am not able to understand how to iterate this in the component file as ...
5
votes
4answers
2k views

Reverse the order of map in Apex or VF

How can I loop through a Map or List in reverse order in VF? or Can I reverse the order of a map in Apex? Here is my code: String nsp = PackageUtils.namespacePrefix; Map<String, String&...
4
votes
3answers
8k views

Access apex Map in Javascript

I'm trying to access a the apex Map named "resultsMap" with javascript, I would like to get contact last name using contact id. public class AccessMapJS { public Map<ID, Contact> ...
3
votes
2answers
181 views

Clarification on maps in Salesforce

I know this might be a very basic question but i'm pretty confused by this: Map<Id,Account> accountWithOpportunities = new Map<Id,Account>([SELECT Id,(SELECT Id FROM Opportunities) ...
0
votes
1answer
1k views

Tree Structure - Hierarchy Structure in visual force page

Good Old question but unable to figure out a easy way. can some one please guide me if anyone has faced this. I am working on dislaying a complex tree-branch-leaf structure.All the Tree-Branch-Leaf ...
0
votes
1answer
427 views

Translate spanish characters to english for field values

I have a requirement to translate few predetermined spanish characters to english in field values of different custom objects. I have tried to create a generic code. can someone please let me know ...
-1
votes
2answers
8k views

How to add value in map of map?

I am trying to add map of map values, I have to intialize the map key first and then try to add value later. Below is the exact logic i am trying to achieve. Map<string,Map<string,List<...
30
votes
1answer
34k views

Initialize Map<String, Integer>

Is there a way to initialize a Map<String, Integer> without using put method ? I saw that for initalize a Map<String, String> : private Map<String, String> myMap = new Map<...
11
votes
1answer
1k views

What is the correct JSON/Apex Strategy?

Summary I'm having a lot of trouble deserializing a JSON file into apex. The problem is not in how to deserialize a JSON string, but rather what type of apex objects I should be using. I don't come ...
10
votes
2answers
2k views

What is the best way to cast/convert Map<String, SObject> to Map<Object, Object> and back again?

To reduce code duplication, at the risk of type safety, I have some method: public Map<Object, Object> doSomething(Map<Object, Object> inputMap) { Map<Object, Object> outputMap; ...
26
votes
2answers
55k views

Iterating over a map (Apex)

I have a map written in Apex. Map<Id, Set<String>> emailDiseaseMap = new Map <Id, Set<String>> (); The map contains an ID and then a set of email addresses. I am trying to ...
4
votes
4answers
11k views

Map of get vs containskey efficient check?

I have map Map sampleMap ; I have little confusion on the below two map methods. i got a review saying that to use containsKey method than get method. But i am thinking in a way that the get will ...
15
votes
2answers
10k views

How to send Javascript map as a parameter from lightning component to Apex server controller?

I have a lightning component which enqueues server side action. To that action I need to pass a map as one of the parameters, but it looks like it takes memory address of the object instead. The ...
10
votes
4answers
2k views

Is there a way to generate a unique hashcode for an in memory SObject that does not yet have an Id?

When SObjects are used as Map keys they are compared by field values, so for example: Account a = new Account(); Account b = new Account(); System.debug(a == b); // Prints true Map<SObject, ...
11
votes
1answer
1k views

What's In A Hash Collision?

When I have used Custom Types in Map Keys and Sets, I generally follow a similar pattern listed in the Apex Developer Guide: public Integer hashCode() { return (31 * x) ^ y; } Someone asked ...
10
votes
2answers
3k views

How deep can nested Maps go, and how do I properly add data to those nested maps?

Background: Long story short, my company is integrating with a platform that has a "Role Hierarchy", so to speak, but that Hierarchy isn't stored in Salesforce. To get the "roles" I have to query ...
8
votes
3answers
487 views

Adding a System.debug changes code behavior

I have the following code Map<Account, Integer> accountsMap = new Map<Account, Integer>(); Account a = new Account(); accountsMap.put(a, 1); a.name='Bob'; //System.debug(accountsMap); ...
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); ...
9
votes
1answer
5k views

Using a Map type attribute in aura:if

Trying to create a custom Navigation Bar and display menu entries based on user privileges. I defined this attribute in my component: <aura:attribute name="navItemPrivs" type="Map" default="{ ...
4
votes
1answer
6k views

Aura Attribute of Type Map is not working

I have declared an auro Map attribute in my Lightning Component but when i try to set key value pair to the Map in CLient-Side Controller it throws null error. COMPONENT CODE : <aura:component ...
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
1k views

How do I make approval comments mandatory?

I have a trigger to make rejection comments mandatory. I have many records that have already been approved/rejected without comments. When a user attempts to reject a record that has already been ...
0
votes
1answer
1k views

SOQL query with Map keyeset

I need help in correcting this code.newbie to apex.I want to remove sessions with matching session_id__c from TrainSessions.I have written code like this map<String, GED__c> NonGEDs= new map<...
4
votes
3answers
3k views

Map of Accounts indexed by Opportunity Id

I'm writting a code at Opportunity's before insert trigger. There, I want to create a Map of Account's related to all Trigger.new oportunities indexed by theirs opportunity ID. I expect that each Map ...
4
votes
2answers
206 views

PageReference Parameters Overwritten Even With Different Case?

I ran into this issue and believe this is a Salesforce bug, and I am looking for confirmation. Maps are supposed to be key case sensitive but when we use the pageReference.getParameters.put() it does ...
3
votes
3answers
1k views

Calling .put on Map of Custom Settings cause 'Collection is read only' Apex error

I create a map of Custom Settings and then try to add a new one to it. But it throws the error AssignByLeadSource: execution of BeforeInsert caused by: System.FinalException: Collection is read-...
2
votes
2answers
694 views

When attempting to use a Map I get “System.NullPointerException: Attempt to de-reference a null object”

I am attempting to get a map of a custom object so I can display every record of that object on a visualforce page. APEX Code: Map<Integer, Package__c> packageMap { get; set; } public Map<...
2
votes
2answers
2k views

Removing the keys in a map in a loop context would reduce the map size?

I am trying to reduce the searching area inside a loop once i have satisfied some condition. That means i want to reduce the size of the map by removing the keys in it. Those keys and the related data ...
2
votes
1answer
100 views

Configurable Fields that can be showed on visualforce page

I got this configurable fields from Event__c. The configurable fields would be the basis on what fields that will be available on Event Booker Information its object is the Event_Booking__c. My ...
1
vote
1answer
151 views

Assign Set of Ids in map at one go

I have below piece of code where I want to traverse to details from the base query on master object. Apex code Map<Id,set<Id>> MapOfAcctListItem = new Map<Id,Set<Id>>(); for(...
1
vote
1answer
425 views

Update siblings and parent record in the same after update trigger

I have written a code to update all the sibling's and parent contact's field when one of the child records have been updated. The logic here is to make checkbox false on all the siblings and parent ...
1
vote
1answer
73 views

Do Maps have an Reliable Relationship between keySet() order and values() order?

Let's say I have a map, for example: Map<SObjectField, SObjectField> valueByKeyMap Assuming neither the Map nor its contents are mutated by other activity in the system, can I rely on these ...
1
vote
2answers
889 views

Complicated FOR loop nesting. Having trouble cleaning up legacy code

I am trying to tidy up some really bad legacy code from an old developer and I'm trying to get rid of these nested for loops. The code below shows what I'm currently working with. I've fixed this ...