On Salesforce, the term “map” refers to a set of key value pairs, meaning that it can govern data ranging from primitive data, Apex objects, sObjects, among others. When it comes to skillfully using Salesforce map methods, there is an abundance of options.
With just the touch of a key, a map can be used to find whatever you’re looking for, making it a very useful aspect of Salesforce, but it’s worth noting that some keys have duplicate functions. Therefore, it’s crucial to understand which map is responsible for which function. Below we’ll cover an explanation of the maps, as well as the map methods.
Salesforce Map Methods
The storage of records is one of the most common uses for the maps, as it it used for the purpose of storing records that you would like processed. Most commonly, the act of querying codes, and storing them as a map, is done in order to easily and quickly execute the action you want.
- Creating a query can be performed where the key is the ID of the record, so all you’ll need is the ID for this kind of Map.
- You can either create the Map from scratch, or use an already made snippet, which poses as a template.
- Should you choose to use the snippet, simply copy and paste it into the System Log of your browser, and it will automatically run.
Writing triggers is another function of Maps using Salesforce. Before every update, and following every update trigger, there is usually a Map present. There are two kinds of Maps you can see: the oldMap and the newMap.
- In the oldMap, the list of all the sObjects before modification is available.
- However, in the newMap, the list off all the sObjects after the values have been modified and updated are what you see.
- The reason why the Maps are used in the trigger, is to help execute a form of comparison in the trigger that is being written. Apart from application in the previously mentioned cases, Maps are also commonly used to stay within limits and governors.
This includes but is not limited to:
- UI elements in Salesforce
- Field lengths of Salesforce objects
- Desktop integration clients
- Your personal Salesforce contract
Keep in mind, that some limits may not even apply to your organization. For more information on the limits that apply to Salesforce, see the Salesforce Developer Limits Quick Reference.
If you still have questions regarding triggers in the context of Salesforce, check out Salesforce trigger best practices.
Salesforce Maps Features
The uniqueness of the user is generally determined by the equals and hashcode methods. The uniqueness of the non-primitive keys, such as sObject keys, among other keys in Maps are determined by doing a full-on comparison of the saved value of a given object.
When using the Map key type of Strings, it is important to remember the following:
- They are usually case-sensitive. Two keys may appear to be different, simply because they are defined by the case. The case-sensitive nature of the string type map keys, (otherwise known as two keys with different cases) will have respective entries in the Map.
- Salesforce Map methods like put, get and contain generally treat these case-sensitive keys as very different from one another, so it’s important to keep that in mind while using Map.
The Salesforce Map Methods To Use
There are several methods that can be used on Salesforce. Some of them are quite common, while others you’ll rarely ever use, because they are not commonly utilized. The list below comprises of some of the most common Salesforce Map methods that you will encounter:
- – clear (): this is a map method that is used when you want to clear the entire key –values mappings from your map. This salesforce map method is used almost in all applications.
- – clone (): The clone is another common mapping method that has several applications. It is used when you want to make duplicate copies of the map you are creating.
- – containsKey (Object) : This key is used to return true value if the mapping contains the value that has been specified in the key. The value that determines whether it returns true or not is placed inside the brackets.
- – deepClone () : Just like the clone, the deep clone is used to make duplicate copies. The difference is that the deep clone makes duplicate copies of the map including the sObject. This happens if the map has sObject values in it.
- – equals (Map) : The equal salesforce map method is used to compare different maps. It compares given specified maps and returns true if the maps that are being compared are equal.
- – get (Object) : This salesforce map method is used tp source a given value or object from the map. If the specified value does not exist in the map, the method returns null.
- – getSObjectType (): In this method, what is returned is the token of the sObject that is part of the map value.
- – hashCode () : The hash code is also commonly used. This salesforce map method returns the hash code corresponding to this map.
- – isEmpty () : When used in mapping the isEmpty command returns true if the map contains pair of keys that have zero values.
- – keySet () : This is a command which returns a set which contains all the keys that are available in the Map where it has been used.
- – put (Object, Object) : This method is used to associate a specified value with a give specified key that exists within the map stricture.
- – putAll(Map) : This method functions by copying all the mappings that are available from a given specified Map to the original map. It takes all the keys that are available in the specified map and then makes a copy of them in an original map.
- – putAll (sObject): This function or method is used to add all the sObjects that are contained in a list. The sObjects that are added must be those that are declared as Map<ID, sObject> and Map<String.
- – remove (Key) : Remove key is another method that is commonly used. This method removes the mapping value of a specified key from the map. This happens when the key is present in the map. It goes ahead to return the value of that particular stated key.
- – size () : The size method is used to return the number of key value pairs that are contained in that particular map.
- – values () : It returns a list which has all the values in a map. The values are returned in an arbitrary order.
As in every new skill or program using Salesforce Map methods with skill, and in your favor requires a basic understanding of each of the program’s functions, plus a little bit of practice. More elaborate information on Salesforce is available on the triggers in Salesforce tutorial page, so if you feel you need to brush up on your general Salesforce knowledge, go ahead and review the above link.