You have only declared the Map and never initialized it before adding values into it. You have to initialize the IdValueMap in the start method or Constructor as below and you will not get the Exception.

IdValueMap = new Map<id , map<String, String>>();
Answer from Saroj Bera on Stack Exchange
🌐
Stack Overflow
stackoverflow.com › questions › 58967434 › how-to-concatenate-two-maps-in-apex
concatenation - How to concatenate two maps in apex - Stack Overflow
I agree with @KDH in that, I don't understand the question, you stated that your maps are Map<String, String> so what do you mean by "without merging similar fields or keys"? nerdy beast – nerdy beast · 2019-12-03 22:54:10 +00:00 Commented Dec 3, 2019 at 22:54 · Add a comment | Sorted by: Reset to default · Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 0 · You can use the putAll(fromMap) method : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm ·
Discussions

apex - Maps: adding more (related) sObjects than key values using PUTALL method - Salesforce Stack Exchange
Here's the scenario: you've loaded a bunch of account IDs as keys into your map. But now, instead of loading account records as the sObject values, you're going to load a related object, such as More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
April 15, 2017
APEX: How to put Map inside map - Salesforce Stack Exchange
I have a Map defined inside another Map. I am trying to put values inside both the Maps but Sub Map is not working. Please suggest. Code- Map myMap = new Map More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
September 9, 2022
apex - How to add value in map of map? - Salesforce Stack Exchange
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 More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
java - Apex: map.Put() byVal instead of byRef? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
Deepika Khanna
mytutorialrack.com › home › understanding map methods in apex: key concepts and best practices
Understanding Map Methods in Apex: Key Concepts and Best Practices
June 1, 2023 - Map<String, Integer> studentScores ... studentScores.put('Michael', 78); In Apex, the putAll(fromMap) method is used to add all key-value pairs from one Map object to another....
🌐
JanbaskTraining
janbasktraining.com › home › salesforce maps: features, benefits & methods
Salesforce Maps: Features, Benefits & Methods
December 29, 2022 - A map is a collection of key-value pairs where each unique key maps to a single value. Map keys can be any primitive data type like string, integer,decimal, Id while values can be a primitive, sObject, collection type or an Apex object.
Top answer
1 of 3
8

PutAll only works on the ID field of an SObject. It is syntatic sugar for:

for(SObject record: someList)
    someMap.put(record.Id, record);

To use any other type of mapping, you would have to make your own for loop.

Such a for loop would look like:

Map<Id, Contract[0]> someMap = new Map<id, contract[0]>();

... load account ids here ...

for(Contract record: [SELECT Id, AccountId, random_field FROM Contract WHERE AccountId IN :someMap.keyset()]) {
    if(someMap.get(record.AccountId) == null) {
        someMap.put(record.AccountId, new Contract[0]);
    }
    someMap.get(record.AccountId).add(record);
}

In your specific case, I might consider using a subquery:

someMap.putAll([SELECT Id, (SELECT Id, random_field FROM Contracts) FROM Account WHERE Id IN :someMap.keySet()]);

You can access a list of contracts via:

for(Contract record: someMap.get(accountId).Contracts) ...
2 of 3
2

So, basically, you would like to have a map of key-value pairs, where key is the AccountID, and the value is list of Contracts related to the Account? If so, first of all, rather than having Map < id, contract >, what you are really looking is Map < id, List < Contract > >.

Further on, you could store the list of id's - in the list. And only then use the list of ids to query the contracts and populate the map:

map<id, contract> accIDtoContract = new map<id, List<contract>>();

List<id> accIDs = new List<id>();
for( Usage_Data__c UD : Usage_Data_List ){
    // Don't store AccountID in the list if it was already stored
    if(accIDs.contains(UD.Account__c))
        continue;

    accIDs.add(UD.Account__c);                      
    accIDtoContract(UD.Account__c, new List<Contract>());
}

List<contract> contracts = [SELECT ID, AccountID, random_field FROM Contract WHERE AccountID IN :accIDs]);

for(Contract c : contracts) {
    accIDtoContract.get(c.AccountID).add(c);
}
Top answer
1 of 2
5

First, Maps are defined with two data types, such that we would express this in generic terms written with a unique letter for each data type involved: Map<T, U>, or as the documentation states: Map<T1, T2>. I prefer the classic nomenclature, but choose whichever you feel you understand better.

In your case, T is Id, and U is Map<T′, U′>. T′ and U′ are the type String. If you remember that, it should be easier to construct complicated types in the future:

Map< T ,            U          >
Map< Id, Map<   T′  ,   U′   > >
Map< Id, Map< String, String > > 

Second, to populate nested structures, you have to put them inside each other. In other words, before you can populate a value in the inner map, the outer map must have the object created first. To create most objects, we use the new keyword. One new is one new object.

When we create a new object, we call a constructor, which can take zero or more parameters to configure the initial state of the object. They are normal methods, which means we need to pass in any parameters, like we would any other method. new Map<Id, Map<String, String>>(), for example, constructs a new object with the compound data type Map<Id, Map<String, String>>.

So, to populate such a Map, we would do something like:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).put('value1','value2');

Note that put displaces the previous value if it exists, so we normally check first:

Map<String, String> myMapValue = myMap.get(obj.Id);
if(myMapValue == null) {
  myMapValue = new Map<String, String>();
  myMap.put(obj.Id, myMapValue);
}
myMapValue.put('value1','value2');

You can initialize a Map with the {} constructor format:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>> {
  obj.Id => new Map<String, String> {
    'value1' => 'value2'
  }
};

Notice how there are two new keywords. We are still creating new objects, just placing one inside the other initially. The => operator simply means "key => value." This is the only use of => in Apex at this time.

Note that putAll is used to put a bunch of values inside the map at once. It requires that the parameter provided is the same type as U from our example above.

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
Map<String, String> params = new Map<String, String>();
params.put('value1','value2');
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).putAll(params);
2 of 2
1

Try this

    Map<Id,Map<string,string> myMap = new Map<Id,Map<string,string>{}
myMap.put(obj.Id, new map<string, string>{'test' => 'test'});

More details here : https://stackoverflow.com/questions/9521740/apex-map-within-a-map

🌐
Stripathi
stripathi.com › overview-of-map-in-salesforce-apex
Overview of Map in Salesforce Apex | Suraj Tripathi
May 29, 2023 - putAll(sobjectArray): Appends the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
Find elsewhere
🌐
scotthung
scotthung.wordpress.com › 2014 › 07 › 09 › apex-maps-putall-method-documentation-error
Apex map’s putAll method documentation error | scotthung
July 9, 2014 - As I was refactoring some code, I ran into a documentation bug causing me to chase my tail for a while. The putAll() method can take a map as a parameter and the documentation says (italics are mine):
🌐
SFDC Kid
sfdckid.com › 2019 › 04 › salesforce-apex-collection-map.html
APEX COLLECTION: Map - SFDC Kid
Now unlike list and set, there are attributes for Map as well. Following are the attributes of apex collection Map : clear() : It removes all of the key-value mappings from the map. clone() : It makes a duplicate copy of the map. containskey(key) : It will return true if the map contains a mapping for the specified key.
🌐
CRS Info Solutions
crsinfosolutions.com › home › collections in apex: lists, sets, and maps
Collections in Apex: Lists, Sets, and Maps
Salesforce Training - Explore the different types of collections in Apex: Lists, Sets, and Maps. Find out how they can help you organize and manage your data effectively. I have enrolled for Salesforce Admin and development online course at CRS info solutions. It’s really the best training i have ever taken and syllabus is highly professional
Rating: 5 ​
🌐
Salesforce Developers
developer.salesforce.com › forums
Feed Item Detail | Salesforce Trailblazer Community
March 9, 2016 - Skip to main content · Register now for TDX! Join the must-attend event to experience what’s next and learn how to build it
🌐
https://s2-labs.com/
shreysharma.com › home › map initialization methods in apex
Map Initialization Methods In Apex | Salesforce Developer Tutorials
April 27, 2026 - In Salesforce Apex, Maps are collections of key-value pairs, where each unique key maps to a single value. Initializing Maps in Apex can be done in various ways depending on the requirements.
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54V00007T4TaySAF
Map Method putAll not concatenating - Trailhead - Salesforce
Skip to main content · Take our 10-minute Community Survey. Open now through July 19. Click here to participate
Top answer
1 of 1
4

Taking time to try to understand the error message you're getting will help you understand how to move forward.

Although you have paraphrased the error and not included the stack trace (when asking about errors, you should never try to shorten it or paraphrase it. You don't know what the important part of the error is, and have a fair chance of leaving out vital information), it's pretty clear to experienced eyes that the issue is the following line

queriesList.get(queries) = new Map<Id,SObject>((List<SObject>)database.query(transformedQuery));

Explaining the error

The queriesList.get() part evaluates to a map, so that line of code ends up being
[A Map] = [A different Map];
which is not valid syntax (hence your error).

Variables can be assigned, but what you have here isn't a variable, it's an instance of a Map object.

Correcting that line of code

Instead of attempting to use the assignment operator, since you're working on an instance of a Map, you should be using one of the methods offered by the Map class.

The putAll() method can take a list of SObjects when the map is a Map<Id, SObject> (as you have), so that's what you should use here.

You also are doing some unnecessary casting (database.query() already returns a List<SObject>), so in the end you want that line to be something like
queriesList.get(queries).putAll(database.query(transformedQuery));

Other considerations

Your variable naming needs improvement. queriesList is a Map, not a List, and accQuery isn't a helpful description (it doesn't tell you what its purpose is)

Also, be aware that collections stored inside of collections are stored by reference. That means that right now, each query you store in your queriesList is storing the same map instance. If you have more than one query, the results for each individual query would appear in the map for every query string key.

At the very least, you would want to .clone() your accQuery map when you .put() a new query into the queriesList map.

Cloning gives you an independent copy, so that each map stored by the query template would only store the results of that one query.

🌐
Intellipaat
intellipaat.com › home › blog › what are the map methods available in salesforce?
What are the Map Methods Available in Salesforce? - Intellipaat
April 3, 2025 - The term “map” on Salesforce refers to a collection of key-value pairs, so it can control data types like primitive data, Apex objects, and sObjects, among others.