{"id":386,"date":"2020-07-01T10:23:56","date_gmt":"2020-07-01T10:23:56","guid":{"rendered":"https:\/\/www.winstechnologies.com\/blog\/?p=386"},"modified":"2020-07-01T10:36:43","modified_gmt":"2020-07-01T10:36:43","slug":"map-in-salesforce","status":"publish","type":"post","link":"https:\/\/www.winstechnologies.com\/blog\/map-in-salesforce\/","title":{"rendered":"How Important is to Know About the Map in Salesforce in 2020?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>About Salesforce<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.salesforce.com\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Salesforce <\/a>is a cloud-based online platform for CRM (Customer Relationship Managements). Salesforce provides a Single Integrated CRM platform to manage various departments of a business like marketing, sales, commerce, and service.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What exactly is an Apex Map in Salesforce?<\/strong><\/h2>\n\n\n\n<p>Basically Apex is an Object Oriented programming language, Using that developers execute the flow and transaction control statements on Salesforce Servers using API calls. Almost all syntax are similar to Java and act like DB stored procedures. This feature enables the developers to add business logic into the most system events, including button clicks, related record updates, and Visualforce pages.&nbsp;&nbsp;<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is Apex Collection?<\/strong><\/h3>\n\n\n\n<p>Apex Collections is a type of variable, it can store multiple records. In Apex there are different types of Collections:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>List<\/li><li>Set, and<\/li><li>Map.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Map in Salesforce<\/strong><\/h3>\n\n\n\n<p>Map in Salesforce is a key-value pairs collection, where each unique key maps to a single value. Keys and values can be any data type \u2014 primitive types, collections, Objects, user-defined types, and built-in Apex types. String Type Map keys are case-sensitive. Map methods (like put, get, containsKey, and remove) treat these keys as distinct. The map gives value on the basis of key. Key is always unique but the key value can be duplicated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Maps Methods <\/strong><\/h3>\n\n\n\n<p>The Map methods are all instance methods, that is, they operate on a particular instance of a map.<br>The following are the maps instance methods:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Map Method Syntax.<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;integer, string>exMap= new map&lt;integer, string>();\nexMap.put(1,'James');\nexMap.put(2,'Daniel');\nexMap.put(3,'Peter');\nstring name= exMap.get(1);\nstring name1= exMap.get(2);\nstring name2= exMap.get(3);\nsystem.debug(name);\/\/James\nsystem.debug(name1);\/\/Daniel\nsystem.debug(name2);\/\/ Peter\nset&lt;integer>key= exmap.keyset();\nlist&lt;string>values=exmap.values();\nsystem.debug(values);\/\/James, Daniel, Peter\n<\/code><\/pre>\n\n\n\n<p><strong>Map Methods<\/strong><\/p>\n\n\n\n<p>The following are the main methods for Map. All the following are instance methods.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>clear()<\/strong> Removes all of the key-value mappings from the map.<\/li><li><strong>clone()<\/strong> Makes a duplicate copy of the map.<\/li><li><strong>containsKey(key)<\/strong> returns true if the map contains &#8211; the&nbsp; If the key is a string, the key value is case-sensitive.<\/li><li><strong>deepClone()<\/strong> This method creates a duplicate copy of a map, including sObject records if the map has sObject record values .<\/li><li><strong>equals(map2)<\/strong> It compares a map with a specified map and returns true if both maps are equal; otherwise, returns false.<\/li><li><strong>get(key)<\/strong> This method returns the value of the specified key which is mapped, or it returns null if the map contains no value for this key.<\/li><li><strong>getSObjectType()<\/strong> This method returns the value of the sObject type in format of key and value.<\/li><li><strong>isEmpty()<\/strong> If the map has zero key-value pairs it returns true.<\/li><li><strong>values()<\/strong> This method returns a list which contains all the values in the map.The values are returned in an arbitrary order.<\/li><li><strong>keySet()<\/strong> Returns a set that contains all of the keys in the map.<\/li><li><strong>put(key, value)<\/strong> Associates the specified value with the specified key in the map.<\/li><li><strong>putAll(fromMap)<\/strong> Copies all of the mappings from the specified map to the original map.<\/li><li><strong>putAll(sobjectArray)<\/strong> Adds the list of sObject records to a map declared as Map&lt;ID, sObject&gt; or Map&lt;String, sObject&gt;.<\/li><li><strong>remove(key)<\/strong> This method removes the mapping for the specified key from the map, if present, and returns the corresponding value.<\/li><li><strong>size()<\/strong> Returns the number of key-value pairs in the map.<\/li><\/ul>\n\n\n\n<p><strong>Keys:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>get(key):<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>list&lt;account>myAccounts = new list&lt;account>();\nmyAccounts = [Select ID, Name from Account limit 10]; \nmap&lt;id,string>myAMap = new map&lt;id,string>();\nfor ( Account a : myAccounts ){\n\/\/Here putting account Id and name to map\nmyAMap.put(a.ID, a.Name);\n    } \nfor ( ID aID : myAMap.keySet() ){\nsystem.debug(loggingLevel.debug, myAMap.get(aID));\n    }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>putAll(fromMap):<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, String> map1 = new Map&lt;String, String>();\nmap1.put('Red','LightRed');\nMap&lt;String, String> map2 = new Map&lt;String, String>();\nmap2.put('Blue','DarkRed');\n\/\/ Add map1 entries to map2\nmap2.putAll(map1);\nSystem.debug(map2.values());\/\/(LightRed, 'DarkRed')<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>remove(key):<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, String>colorCodes =   new Map&lt;String, String>();\ncolorCodes.put('Red', 'FF0000');\ncolorCodes.put('Blue', '0000A0');\ncolorCodes.put('Green', '0000A0');\nSystem.debug('printing=== '+colorCodes);\/\/ Blue=0000A0, Green=0000A0, Red=FF0000\nfor(String key:colorCodes.keySet().clone()) {\ncolorCodes.remove('Green');\nsystem.debug(colorCodes);\/\/ Blue=0000A0, Red=FF0000\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Values ():<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, String>colorCodes = new Map&lt;String, String>();\ncolorCodes.put('FF0000', 'Red');\ncolorCodes.put('0000A0', 'Blue');\nList&lt;String> colors = new List&lt;String>();\ncolors = colorCodes.values();\nsystem.debug(colors);\/\/(Red, Blue)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>getSObjectType()<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Map &lt;id,Account>acctMap = new map&lt;id,account>([select  name from  Account limit 10]);\nfor(string accValue:acctmap.keyset()){\nsystem.debug(acctmap.get(accValue));\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> Example How to display record on basis of alphabet?<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Apex class\npublic class DynamicSearchExample {\npublic Map&lt;string,list&lt;account>>accountsMap {get; set;}\npublic List&lt;selectoption> keys {get; set;}\npublic String selectedKey {get; set;}\npublic Map&lt;string, account>accsByName {get; set;}\npublic Set&lt;string>getMapKeys(){\nreturnaccountsMap.keySet();\n    }\npublicDynamicSearchExample(){\naccsByName=new Map&lt;string, account>();\n        List&lt;string>sortedKeys=new List&lt;string>();\naccountsMap=new Map&lt;string, list&lt;account>>();\naccountsMap.put('All', new List&lt;account>());\n        List&lt;account>accs=[select Name, industry,type,phone from Account order by Name asc];               \nfor (Account acc : accs){\naccountsMap.get('All').add(acc);\n            String start=acc.Name.substring(0,1);\n            List&lt;account>accsFromMap=accountsMap.get(start);\nif (null==accsFromMap){\naccsFromMap=new List&lt;account>();\naccountsMap.put(start,accsFromMap);\n            }\naccsFromMap.add(acc);\naccsByName.put(acc.name,acc);\n        }\nkeys=new List&lt;selectoption>();\nfor (String key : accountsMap.keySet()){\nif(key != 'All'){\nsortedKeys.add(key);\n            }\n        }\nsortedKeys.sort();\nsortedKeys.add('All');\nfor (String key : sortedKeys){\nkeys.add(new SelectOption(key, key));\n        }\nselectedKey='All';\n    }\n}\nVisualforce page\n&lt;apex:page controller=\"DynamicSearchExample\">\n&lt;img src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-wp-preserve=\"%3Cscript%3E%0Afunctionredraw_accounts()%7B%0A%0A%20%20%20%20%7D%0A%3C%2Fscript%3E\" data-mce-resize=\"false\" data-mce-placeholder=\"1\" class=\"mce-object\" width=\"20\" height=\"20\" alt=\"&lt;script>\" title=\"&lt;script>\" \/>\n&lt;apex:form>\n&lt;apex:actionFunction name=\"redraw_accounts\" rerender=\"accs\" status=\"status\"\/>\n&lt;apex:pageBlock title=\"Criteria\">\n&lt;apex:outputLabel value=\"Starting Letter\"\/>\n&lt;apex:selectList value=\"{!selectedKey}\" size=\"1\" onchange=\"redraw_accounts()\">\n&lt;apex:selectOptions value=\"{!keys}\" \/>\n&lt;\/apex:selectList>\n&lt;\/apex:pageBlock>\n&lt;apex:pageBlock title=\"Accounts\">\n&lt;apex:actionstatus id=\"status\" startText=\"Loading Data...........\" stopText=\"Loading completed.....\" >\n&lt;apex:facet name=\"start\"\/>\n&lt;apex:facet name=\"stop\">\n&lt;apex:outputPanel id=\"accs\">\n&lt;apex:pageBlockTable value=\"{!accountsMap[selectedKey]}\" var=\"acc\">\n&lt;apex:column value=\"{!acc.name}\"\/>\n&lt;apex:column value=\"{!acc.industry}\"\/>\n&lt;apex:column value=\"{!acc.type}\"\/>\n&lt;apex:column value=\"{!acc.phone}\"\/>\n&lt;\/apex:pageBlockTable>\n&lt;\/apex:outputPanel>\n&lt;\/apex:facet>\n&lt;\/apex:actionstatus>\n&lt;\/apex:pageBlock>\n&lt;\/apex:form>\n&lt;\/apex:page><\/code><\/pre>\n\n\n\n<p><strong>Output of the above program: <\/strong><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"201\" class=\"wp-image-388\" style=\"width: 500px;\" src=\"https:\/\/www.winstechnologies.com\/blog\/wp-content\/uploads\/2020\/07\/3-8.png\" alt=\"map in salesforce\" srcset=\"https:\/\/www.winstechnologies.com\/blog\/wp-content\/uploads\/2020\/07\/3-8.png 716w, https:\/\/www.winstechnologies.com\/blog\/wp-content\/uploads\/2020\/07\/3-8-300x121.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>About Salesforce Salesforce is a cloud-based online platform for CRM (Customer Relationship Managements). Salesforce provides a Single Integrated CRM platform to manage various departments of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-salesforce","category-tech-trends","entry"],"_links":{"self":[{"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":3,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.winstechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}