12
MarThe Map methods are all instance methods, that is, they operate on a particular instance of a map.
The following are the instance methods for maps:
What is a map?
Map<integer, string>exMap= new map<integer, string>();
exMap.put(1,'James');
exMap.put(2,'Daniel');
exMap.put(3,'Peter');
string name= exMap.get(1);
string name1= exMap.get(2);
string name2= exMap.get(3);
system.debug(name);//James
system.debug(name1);//Daniel
system.debug(name2);// Peter
set<integer>key= exmap.keyset();
list<string>values=exmap.values();
system.debug(values);//James, Daniel, Peter
Once you’re familiar with the syntax and the fundamentals of Salesforce, mapping with it becomes simpler. Learn more with our Self-learning module.
Read: What Are The Differences Between Sugarcrm & Salesforce?
Learn Salesforce in the Easiest Way
The following are methods for Map. All are instance methods.
Read: How to Prepare For Salesforce App Builder Certification?
list<account>myAccounts = new list<account>();
myAccounts = [Select ID, Name from Account limit 10];
map<id,string>myAMap = new map<id,string>();
for ( Account a : myAccounts ){
//Here putting account Id and name to map
myAMap.put(a.ID, a.Name);
}
for ( ID aID : myAMap.keySet() ){
system.debug(loggingLevel.debug, myAMap.get(aID));
}
Map<String, String> map1 = new Map<String, String>();
map1.put('Red','LightRed');
Map<String, String> map2 = new Map<String, String>();
map2.put('Blue','DarkRed');
// Add map1 entries to map2
map2.putAll(map1);
System.debug(map2.values());//(LightRed, 'DarkRed')
Map<String, String>colorCodes = new Map<String, String>();
colorCodes.put('Red', 'FF0000');
colorCodes.put('Blue', '0000A0');
colorCodes.put('Green', '0000A0');
System.debug('printing=== '+colorCodes);// Blue=0000A0, Green=0000A0, Red=FF0000
for(String key:colorCodes.keySet().clone()) {
colorCodes.remove('Green');
system.debug(colorCodes);// Blue=0000A0, Red=FF0000
}
Salesforce Training For Administrators & Developers
Map<String, String>colorCodes = new Map<String, String>();
colorCodes.put('FF0000', 'Red');
colorCodes.put('0000A0', 'Blue');
List<String> colors = new List<String>();
colors = colorCodes.values();
system.debug(colors);//(Red, Blue)
Map <id,Account>acctMap = new map<id,account>([select name from Account limit 10]);
for(string accValue:acctmap.keyset()){
system.debug(acctmap.get(accValue));
}
Example How to display record on basis of alphabet
Read: What is Salesforce? The Intriguing World of Salesforce in 2020
Apex class
public class DynamicSearchExample {
public Map<string,list<account>>accountsMap {get; set;}
public List<selectoption> keys {get; set;}
public String selectedKey {get; set;}
public Map<string, account>accsByName {get; set;}
public Set<string>getMapKeys(){
returnaccountsMap.keySet();
}
publicDynamicSearchExample(){
accsByName=new Map<string, account>();
List<string>sortedKeys=new List<string>();
accountsMap=new Map<string, list<account>>();
accountsMap.put('All', new List<account>());
List<account>accs=[select Name, industry,type,phone from Account order by Name asc];
for (Account acc : accs){
accountsMap.get('All').add(acc);
String start=acc.Name.substring(0,1);
List<account>accsFromMap=accountsMap.get(start);
if (null==accsFromMap){
accsFromMap=new List<account>();
accountsMap.put(start,accsFromMap);
}
accsFromMap.add(acc);
accsByName.put(acc.name,acc);
}
keys=new List<selectoption>();
for (String key : accountsMap.keySet()){
if(key != 'All'){
sortedKeys.add(key);
}
}
sortedKeys.sort();
sortedKeys.add('All');
for (String key : sortedKeys){
keys.add(new SelectOption(key, key));
}
selectedKey='All';
}
}
Visualforce page
<apex:page controller="DynamicSearchExample">
<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="<script>" title="<script>" />
<apex:form>
<apex:actionFunction name="redraw_accounts" rerender="accs" status="status"/>
<apex:pageBlock title="Criteria">
<apex:outputLabel value="Starting Letter"/>
<apex:selectList value="{!selectedKey}" size="1" onchange="redraw_accounts()">
<apex:selectOptions value="{!keys}" />
</apex:selectList>
</apex:pageBlock>
<apex:pageBlock title="Accounts">
<apex:actionstatus id="status" startText="Loading Data..........." stopText="Loading completed....." >
<apex:facet name="start"/>
<apex:facet name="stop">
<apex:outputPanel id="accs">
<apex:pageBlockTable value="{!accountsMap[selectedKey]}" var="acc">
<apex:column value="{!acc.name}"/>
<apex:column value="{!acc.industry}"/>
<apex:column value="{!acc.type}"/>
<apex:column value="{!acc.phone}"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:pageBlock>
</apex:form>
</apex:page>
Output
The reason why maps are essential to use in Salesforce is that it makes it easy for writing the bulk apex class or trigger. As a beginner of Salesforce, we are unaware of various techniques using that we can employ script statements. You can learn more on maps in Salesforce by signing up with JanBask Training. Happy mapping to the happy people!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
AWS
DevOps
Data Science
Hadoop
Salesforce
QA
Business Analyst
MS SQL Server
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Trending Posts
What is SFDC? What does SFDC stand for?
908
What Is The Difference Between Tables And Views In SQL?
563
How to Add A New Column to a Table in SQL?
541
What is the SQL Insert Query? How to Insert (Date, Multiple Rows, and Values in Table)
449
What Is SQL Candidate Key? Difference between Primary Key & Candidate Key
362
Related Posts
Receive Latest Materials and Offers on Salesforce Course
Interviews