You can't refer to properties dynamically like that in Apex.

formSubmission.fs =formSubmissionData.get(fs);

If you want to place a value in the field whose name corresponds to the content of the variable fs, you must use the put() method.

formSubmission.put(fs, formSubmissionData.get(fs));
Answer from David Reed on Stack Exchange
🌐
CRS Info Solutions
crsinfosolutions.com › home › what is a map class in salesforce apex? with examples
What is a Map class in Salesforce Apex? with examples
Salesforce Training - The Map class is a collection type that stores unique keys paired with values. It is used to quickly retrieve, update, or delete data based on a unique identifier, making data handling more efficient and faster than using lists, especially with large data sets. 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 ​
🌐
Camp Apex
campapex.org › lesson › 640d2a156e4efea481c54de2
Map - Keyset and Values
You need to enable JavaScript to run this app · Camp Apex · Courses · Competitions · Projects · Login · Reading Only
People also ask

Are Apex Map keys case-sensitive?
Yes. For String keys, 'Account' and 'account' are treated as two separate keys, even though the Apex == operator compares strings case-insensitively. Normalize the case, for example with toLowerCase(), before using strings as keys if you want case-insensitive behavior.
🌐
micropyramid.com
micropyramid.com › home › blog › salesforce › apex map methods in salesforce: a complete guide
Apex Map Methods in Salesforce: A Complete Guide | MicroPyramid
How do I get all keys or all values from an Apex Map?
Call keySet() to get a Set of every key, and values() to get a List of every value. keySet() is handy for binding into a SOQL IN clause, while values() gives you a list you can iterate or pass straight to DML.
🌐
micropyramid.com
micropyramid.com › home › blog › salesforce › apex map methods in salesforce: a complete guide
Apex Map Methods in Salesforce: A Complete Guide | MicroPyramid
What is a Map in Salesforce Apex?
A Map is an Apex collection of unique key/value pairs. Each key maps to exactly one value, keys must be unique, and both keys and values can be any data type — primitives, sObjects, or custom classes. You use it to quickly look up a value by its key.
🌐
micropyramid.com
micropyramid.com › home › blog › salesforce › apex map methods in salesforce: a complete guide
Apex Map Methods in Salesforce: A Complete Guide | MicroPyramid
🌐
MicroPyramid
micropyramid.com › home › blog › salesforce › apex map methods in salesforce: a complete guide
Apex Map Methods in Salesforce: A Complete Guide | MicroPyramid
June 10, 2026 - The table below lists the most-used Map<K, V> instance methods. keySet() returns a Set<K> — so you can iterate the keys or feed them straight into a SOQL IN bind — while values() returns a List<V> you can loop over or pass to DML.
🌐
Levelupsalesforce
levelupsalesforce.com › convert-map-to-list
Apex Convert Map To List - LevelUpSalesforce
August 30, 2022 - We can easily retrieve all of the Ids from an Apex Sobject Map by using the Map.KeySet() method. This method can be leveraged to return a Set of any type.
🌐
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 ... // returns false · In Apex, the keySet() method of the Map class returns a Set containing all the keys present in the map....
Find elsewhere
🌐
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.
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54V00007T4OlISAV
.keyset() method | Salesforce Trailblazer Community
Skip to main content · Take our 10-minute Community Survey. Open now through July 19. Click here to participate
🌐
Forceshark
forceshark.com › blog › key-concepts-and-usage-of-maps-in-salesforce-apex
Key concepts and usage of Maps in Salesforce Apex | Blog
for (String name : myMap.keySet()) { Integer age = myMap.get(name); System.debug(name + ' is ' + age + ' years old.'); } Maps allow flexibility in data types for keys and values. You can use various data types based on your specific requirements. Internally, many programming languages, including Salesforce Apex, implement maps using hash tables.
🌐
S2 Labs
s2-labs.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.
Top answer
1 of 1
6

Like Adrian mentioned in the comments, it's hard to know how to make your work method "work correctly" when we don't know how the way it's currently working is different from the way you want it to work.

That said, I do have an idea of what may be going wrong (and how to fix it).

First, answering the text of your question the purpose of keySet().contains(<something>) in the following snippet

for(WorkOrder i:newWorkOrder) {
    if(resultMap.keyset().contains(i.ParentWorkOrderId)) {
        WorkOrder wo = resultMap.get(i.ParentWorkOrderId);
        i.related_address__c = wo.Related_Address__c;

        // I've omitted the other lines for brevity
    }                
}

is to make sure that the WorkOrder wo variable inside the if block will never be null. The if statement checks to make sure that your map has an entry for i.ParentWorkOrderId (that the resulting Id exists as a key in the map) before it attempts to use the result of fetching the value in the map for that given key.

A simple example may make things a bit more clear

// This code can be run as anonymous apex through the developer console so that
//   you can verify, for yourself, the assertions I'll be making.

// Declare a simple map that maps one integer to its negative
// What I'm doing here is one way to initialize a map
Map<Integer, Integer> testMap = new Map<Integer, Integer>{
    1 => -1,
    2 => -2
};

// A simple variable to store the result of a .get() from the map
Integer result;

result = testMap.get(1);
// map.containsKey() accomplishes the same thing as map.keySet().contains().
// using containsKey() is more compact, so it is generally preferred.
debug(testMap.containsKey(1));  // This will result in 'TRUE' being printed in the debug log
debug(result);                  // This will result in a '-1' being printed in the debug log

result = testMap.get(2);
debug(testMap.containsKey(2));  // This will result in 'TRUE' being printed in the debug log
debug(result);                  // This will result in a '-2' being printed in the debug log

// We did not make a map entry for the integer 3
result = testMap.get(3);
debug(testMap.containsKey(3));  // This will result in 'FALSE' being printed in the debug log
debug(result);                  // This will result in a 'null' being printed in the debug log

That's all well and good, but what does the if() block in your code actually guard against?

Let's expand the above example just a little more.

Map<Integer, Integer> testMap = new Map<Integer, Integer>{
    1 => -1,
    2 => -2
};

// A simple variable to store the result of a .get() from the map
Integer result = testMap.get(3);

// The format() method of the integer class returns a string representing the 
//   integer in the current user's locale.
// If integer i = 1000, i.format() would return '1,000' in US and UK locales
//   but would return '1.000' in most other locales in the Eurozone (Germany, for example)
system.debug(result.format());

If you try to run that second code example, you'll get a null pointer exception. Why? because 3 isn't a key in the map, and when we try to fetch the value for key 3 from the map, we get null as the result.

The system.debug line effectively becomes system.debug(null.format());. null doesn't have a format() method (it doesn't have any methods). Trying to use dot notation with null results in a null pointer exception.

Getting back to your code now

for(WorkOrder i:newWorkOrder) {
    if(resultMap.containsKey(i.ParentWorkOrderId)) {
        // because we're checking that i.ParentWorkOrderId exists as a key
        //   in resultMap, wo is guaranteed to not be null (well, as long as
        //   the value stored in the map itself isn't null).
        // This means that wo.Related_Address__c is safe to execute (with one caveat).
        WorkOrder wo = resultMap.get(i.ParentWorkOrderId);
        i.related_address__c = wo.Related_Address__c;

        // I've omitted the other lines for brevity (again)
    }
}

So now you know what the purpose of that line is, we can move to getting your code to behave as expected.

Based on your code, it appears that you're depending on the behavior of before triggers to avoid the need to use DML to update the records you're passing in to copyWorkOrderAddress(). This does require a before trigger, and it also requires that you are passing in a list derived _directly from trigger.new (only changes made to the instances stored in trigger.new get auto-updated without DML).

Since you aren't running into a "record is read-only" error (at least, not yet or not that you've indicated), I can assume that this code is being run as part of a before trigger.

If your issue is that your child WorkOrder records aren't getting address values from the parent record, then you should check to see how you're calling your method.

// This should work, because we're directly using trigger.new, and SObjects/
//   collections of SObjects are passed by reference (more or less, it's a bit more
//   complicated than that in reality)
copyWorkOrderAddress(trigger.new);

// This should also work, because the loop variable 'wo' is a reference
//   to the same instance of the work order record that is stored in trigger.new
List<WorkOrder> myList = new List<WorkOrder>();
for(WorkOrder wo :trigger.new){
    if(someCondition){
        myList.add(wo);
    }
}
copyWorkOrderAddress(myList);

// This will not work, because the instance of the work order record we're
//   storing in myOtherList is not a reference to the instance contained in
//   trigger.new, it's a completely independent instance.
List<WorkOrder> myOtherList = new List<WorkOrder>();
for(WorkOrder wo :trigger.new){
    if(someCondition){
        myList.add(new WorkOrker(
            Id = wo.Id, 
            ParentWorkOrderId = wo.ParentWorkOrderId
        ));
    }
}
copyWorkOrderAddress(myList);

Hopefully I was able to identify the issue you're running into. If that isn't it, or you need more specific help, then posting your trigger and trigger handler would be helpful.

One last thing, that caveat I mentioned. As is, your current code doesn't check to see if i.ParentWorkOrderId is null before adding it to your list of Ids to query against. You should be safe in this case, but if you were to actually add null as a key to your map, you could still end up with a null pointer exception (or other fun, odd results could emerge).

When in doubt though, I would add relatedWOID.remove(null); after the loop that populates the relatedWOID set.

🌐
Iterative Logic
iterativelogic.com › working-with-apex-code-maps
Working with Apex Code Maps - Iterative Logic
May 10, 2015 - As you can see, no loops were needed to get a set of account ids. We simply passed the list of accounts to the new map instance and then used the maps keySet() method to get a list of ids for us. As a bonus, Apex Trigger methods provide a map of sObjects for us.
🌐
SFDC Kid
sfdckid.com › 2019 › 04 › salesforce-apex-collection-map.html
APEX COLLECTION: Map - SFDC Kid
So let's get started, A Map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type (primitive type, collections, sObjects, user-defined types, and built-in Apex types).
🌐
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 - An illustration is StateWithCa... the total number of key-value pairs in the map. keySet(): This function returns the set of keys that the map contains....