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
Irvine DelacroixIrvine Delacroix 

Need help to understand Map collection

User-added image

Hi Experts,

I'd like someone to help me understand how to use Map collection. I've seen some examples, articles and documentions that are too advanced and really hard to understand. Some explanations seems to hard to imagine how to use them in a Real World.

So in the scenario Above, I'd like to update a field from Contact when it's created with a value from other Related list undre Account.

I know it can be done with Maps but I don't know how to start my query or how to apply Maps.

•I'd like to know which one should be the Map
•What's going to be my first step then second then third etc.
•What are the best practices for this.


It's would be awesome if someone can comment based on Experience and not just post some artilces. Mostly, articles are confusing and don't really explain how to use things in the Real world.

P.S.

If it's possible for me to have copy of some sort of documents about Maps or Apex, I'd like to have them in my email. vinzell999@gmail.com :)

Thanks a Bunch! 
Best Answer chosen by Irvine Delacroix
Himanshu ParasharHimanshu Parashar
Hi Irvine,

It is all about preparing the data which is common across the object ans which can be used for mapping. For example
 
ObjectA__c and it's fields

Account__c (Referring Master details with Account)
Fielda__c (Boolean)
Fieldb__c (Text)
Fieldc__c (Text)

Now you want to put data ObjectA__c.Fieldc__c field data on Contact so to do that you can see that ou have Account common between ObjectA__c with Contact right. so you will query ObjectA__c and you will create a where Map's key value will be Accountid and value will be Field__c as shown in below code
 
Map<String,ObjectA__c> AccountMap = new Map<String,ObjectA__c>();
for(ObjectA__c obj : [select id,Account__c,Fieldc__c from ObjectA__c limit ])
{
AccountMap.put(obj.Account__c,obj);
}
Now you can use this AccountMap anywhere in your code and get data by passing Account id
 
for(Contact con : [select id,Accountid,field__c from contact limit 5])
{
   ObjectA__c obj = AccountMap.get(con.Accountid); //Get data from Map
   con.field__c = obj.fieldc__c; //Assign actual value from objectA __c
}


Does it makes sense ?


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

All Answers

Himanshu ParasharHimanshu Parashar
Hi Irvine,

Map is always a key value pair collection as described in below code, you will pass the key and you will get the value of that key.
 
//This Line initialize map with String as a key and String as a value
Map<String,String> Companymap = new Map<String,String>();\
//Add items to the map
Companymap.put('Salesforce','CRM');
Companymap.put('Apple','MOBILE');
Companymap.put('IBM','HARDWARE');

//Get item from the map syntax
System.debu(Companymap.get('IBM')); //This will output HARDWARE

In above example Salesforce,Apple,IBM are keys and CRM,Mobile, Hardware are values. This is basic about Map collection.

Now Lets talk about your problem.

You want to get data from Account object and put on contact object. so the use case will be that you need a Map of Contactid with corresponding Account object so Map will init like this, 
 
Map<String,String> Contactmap = new Map<String,String>();
//Fill Map with data
for(Contact con : [select id,Account.custom__c from contact limit 5];
{
   Contact.put(con.id,con.Account.custom__c);
}

You can get data from this map using 
 
String AccID = ContactMap.get(PAAS THE ID HERE);

Above is the basic example of Map of Contact with single value.

so your task is to create a Map like this
 
Map<String,Account> ContactMap = new Map<String,Account>();
Hope this will help you to understand Map

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
sandeep sankhlasandeep sankhla
Hi Irvine,
This is simple one which you can handle from trigger..I haev few question for this req..

1. Is there any relationship bewteen Conatct and Other object ? if yes then it is very simple..
if not then when we create a contact that time we will check its related account and as it shows in above diagram, Account can have multiple "other Objects", so if which one we should be using for field mapping..

Account A is having --> other Object 1 , other Object 2 , other Object 3

Conatct A we are creating which is related to Account A..

So from 3 related other objects whiched  one we should use for field mapping ? or you need to rollup means aggreagted sum of all related objects..

Or it is somthing like there will be only 1 other object related to account ??

Thanks,
Sandeep
 
Irvine DelacroixIrvine Delacroix
Thanks a lot for the detailed explanation Himanshu, this will help a lot :)

However, I do have another question.

Your example seems to be pulling up value of a field from Account (Account.custom__c). 

What if the value that I am getting is coming from a Related list under account and passing it to the Contact, how do I query that? and after I query it, how do I get the value to pass it to the contact field.

Thanks,

 
Himanshu ParasharHimanshu Parashar
Hi Irvine,

It is all about preparing the data which is common across the object ans which can be used for mapping. For example
 
ObjectA__c and it's fields

Account__c (Referring Master details with Account)
Fielda__c (Boolean)
Fieldb__c (Text)
Fieldc__c (Text)

Now you want to put data ObjectA__c.Fieldc__c field data on Contact so to do that you can see that ou have Account common between ObjectA__c with Contact right. so you will query ObjectA__c and you will create a where Map's key value will be Accountid and value will be Field__c as shown in below code
 
Map<String,ObjectA__c> AccountMap = new Map<String,ObjectA__c>();
for(ObjectA__c obj : [select id,Account__c,Fieldc__c from ObjectA__c limit ])
{
AccountMap.put(obj.Account__c,obj);
}
Now you can use this AccountMap anywhere in your code and get data by passing Account id
 
for(Contact con : [select id,Accountid,field__c from contact limit 5])
{
   ObjectA__c obj = AccountMap.get(con.Accountid); //Get data from Map
   con.field__c = obj.fieldc__c; //Assign actual value from objectA __c
}


Does it makes sense ?


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

This was selected as the best answer
Irvine DelacroixIrvine Delacroix
BIG THANKS @Himanshu! this is really helpful. I can start from here now.. Appreciate it :)

I hope I can get more help from you in the future. You're the best!

Thanks again for sharing :)

-Irvine

 
Irvine DelacroixIrvine Delacroix
Thank you for answering as well @sandeep sankhla,

Himanshu already answered a lot but it would be awesome if you can give me some more Ideas about this :)

Basically, there can be more than 1 records related to the Account. But say for example, there's a picklist field on Contact (value: 1, 2 3) and also same in Related record in Account.

the Value that I want to pass in Contact is the Record from Account Where the Picklist value is the same as the contact field.

Thanks,

-Irvine
sandeep sankhlasandeep sankhla
Hi Irvine,

You mean to say lets take an example

Contact 1 conatct 2 contact2 are related to Account1 

Object 1 Object2 and Object 3 are also related to Account 1

if Contact 1 is having picklist value as 33 and Object 2 is also having picklist value 33 then Object 2 field we should be mapping with contact 1..

Let me know if above is the case..

Picklist are present on Conatc and Other Object ?
Irvine DelacroixIrvine Delacroix
Hi sandeep,

Sorry I didn't respond to you sooner. I'm not full time with studying apex so I can only answer when I get the chance.

Yes, if picklist value from contact 1 is 33 and Object 2 has 33 value as well, then pass the field value from Object 2 to Contac1 :)

Thanks a lot @sandeep
sandeep sankhlasandeep sankhla
Hi Irvine,

Please refer the below trigger code for your requirnmnet....small balnak and null check you can add in this code ..
I have added comments also how we are updating teh values
 
trigger OtherObjectTrigger on otherObject__c (after update) {
	
	set<Id> setAccIds = new set<Id>();//Store all account ids...
	
	Map<String, otherObject__c> mappickValueToOtherObject = new Map<String, otherObject__c>();//In this map you will store Picklist value + AccountId as Key for identifying the correct related contact. In value you will store the OtherObjectData..
	List<Contact> lstContactsToUpdate = new List<Contact>();
	
	//here you will iterate all updated otherObject adn then you can preprae a map like below...
	for(otherObject__c objSite : trigger.new)
	{
		setAccIds.add(objSite.ParentAccount__c);
		mappickValueToOtherObject.put(objSite.FinalValue__c + '--'+objSite.ParentAccount__c ,objSite);
	}

    //here you will query all contact those are related to all accounts whihc you already stored in setAccIds(all account related to other object)
	for(Contact objReview : [Select Id, UpdatedText__c, FinalValue__c, 	Account__c, Name from Contact where Account__c IN:setAccIds])
	{
		//here you will check if same unique key menas picklist value and account id is there in conatct then you will update this contact field with other object field..
		if(mappickValueToOtherObject.containskey(objReview.FinalValue__c + '--'+ objReview.Account__c))
		{
			//here you can update all feilds which you want to update from otherobject to Contact// 
			objReview.UpdatedText__c = mappickValueToOtherObject.get(objReview.FinalValue__c + '--'+ objReview.Account__c).UpdatedText__c;
			lstContactsToUpdate.add(objReview);//store all updated values in a list
		}
	}
	//if list is not empty then update all contacts which new values
	if(!lstContactsToUpdate.isEmpty())
	{
		update lstContactsToUpdate;
	}
}

Please use this code and let me know if it works for you..I have tried in my org and it is working good....You can simply replace the objects Api names and field names and do small null check if nay field is empty or not....

Please check and letme know if you are facing any issue in this..

with above code you will be able tio achive this

If you have there OtherObject records and you are updating them then this code will find related contact record based on common account Id and picklist value and it will update the fields in contact..

please try and let me know if you have nay doubts
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer