function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amogh BabhaleAmogh Babhale 

Map function in trigger

Hi Everyone.

I am currenty taking lessons on the trigger and having hard time to understand the use Map funtion how it is work and where it should be use so far I understand that it is use to get the old value of the record please any one can explain how we can use map in triggers.

It is impotand to learn because it can imporve the eficincncy of the code and we can avoid the writing the SOQL queries inside for loops.

Thanks !!
VinayVinay (Salesforce Developers) 
Hi Amogh,

A map is a collection of key-value pairs where each unique key maps to a single value. In 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.

>> Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
>> Map keys of type String are case-sensitive. Map methods, including put, get, containsKey, and remove treat these keys as distinct.
>> The map gives value on the basis of key.
>> Key is always unique but a value can be duplicated.

Review below links which can give more information.

https://developer.salesforce.com/forums/?id=9060G000000I4s1QAC
http://amitsalesforce.blogspot.com/2016/09/collection-in-salesforce-example-using.html
https://trailhead.salesforce.com/en/content/learn/modules/object-oriented-programming-for-admins/define-sets-and-maps

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar