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
belabela 

can you please explain the map funtionality for below code

Hi All,

Below is the trigger where when contacts are created, there is field called Count_of_Contacts__c  in account which counts no of contacts and display in account field Count_of_Contacts__c.In this I have a doubt regarding map,Please explain me.  

mapcon.put(c.accountid,listcon.size());//In this line we are putting accountids which are in contact object,to get the size of contacts
mapcon.get(ac.id)​       //in this line we are using account reference and getting id for size,but we have to call like this                      know mapcon.get(c.accountid) to get the size,because we have created map which taking accountids of contact,but how it is taking ac.id which is in account for getting size.



trigger contactcountaccount on Contact (after insert) {
list<Id> listContact = new list<Id>();
list<contact> listcon=new list<contact>();
list<account> listAcc= new list<account>(); 

set<Id> s=new set<Id>();
for(contact c:trigger.new){
s.add(c.accountid);
}

list<account> accountId = [select id,name,Count_of_Contacts__c from account where id in:s];
list<contact> contactId=  [select id,accountid,name from contact where accountid in:s];
map<id,Integer> mapcon=new map<id,Integer>();


for(account A:accountId)
{
for(contact C: contactId){
if(c.accountid==a.id){
listcon.add(c);
system.debug('this are list of contacts if ids are equal'+listcon);
mapcon.put(c.accountid,listcon.size());           //please explain
system.debug('this are map of con accountid and consize '+mapcon);


 }
}
}
if(listcon.size()>0){
for(account ac:accountId){
if(mapcon.get(ac.id)==null){                    //please explain
system.debug('GET account id into map'+mapcon.get(ac.id));

ac.Count_of_Contacts__c =0;
}
else{
ac.Count_of_Contacts__c = mapcon.get(ac.id);
system.debug('getting how many contacts with id'+ac.Count_of_Contacts__c);

}
listAcc.add(ac);
system.debug('final list listAcc'+listAcc);

}

}

if(listAcc.size()>0)
update listAcc

Thanks,
B.Mahanandeesh
Best Answer chosen by bela
Amit Chaudhary 8Amit Chaudhary 8
Both are same acc.id means Account Id and c.accountid means account id which is associated with contact. In both case we are using account id get record

All Answers

Veenesh VikramVeenesh Vikram
Hi,

See in this line:
mapcon.put(c.accountid,listcon.size());    

You are creating a Map by iterating over contacts, you get the Id of account with the size of contacts related to it and put it in a map.

After the map has been created, you start iterating over accounts, now in order to check if that account has any contact related to it, yo do this:
mapcon.get(ac.id)

If account ac has any contact related to it, it will not give a null. Actually it doesn matter if the Key passed is of contact's accou nt or Account Id itself. Ultimately the Key is ID type and the mao as Account'Ids in the Keyset, so you pass account Id in any way, you will get corressponding Value depending on the key.

Best Regards
Veenesh



 
Amit Chaudhary 8Amit Chaudhary 8
Please smilfy your code like below
// Add Update insert and delete all event
trigger NumberOfContacts on Contact ( after delete, after insert, after update ) 
{
	Set<Id> accountIds = new Set<Id>();
	if(Trigger.isDelete)
	{
		for(Contact c : trigger.old) // We need to use Tigger.old for Delete
		{
			accountIds.add(c.AccountId); // Get all associated account Id
		}
	}
	else
	{
		for(Contact c : trigger.new) // We need to use Tigger.new all other event
		{
			accountIds.add(c.AccountId); // Get all associated account Id
		}
	}

	if(accountIds.size() > 0 ) 
	{	
		list<account> accLst = new list<account>();	
		// Query all Account and associated Contact
		for(Account objAcc: [select id,Count_of_Contacts__c,(select id from contacts) from account where id In : accountIds]) 
		{
			objAcc.Count_of_Contacts__c = objAcc.contacts.size(); // Get the size of contacts and assign
			accLst.add(objAcc);
		}
	
		if(accLst.size() > 0 )
		{
			update accLst;
		}	
	}	
}
Let us know if this will help you

Thanks
Amit Chaudhary
 
belabela
Hi veenesh,Amit,
Thanks for the response,Can u make it clear,because i want to know about map,My doubt is we are using this  mapcon.get(ac.id) to get the size().this size() will get only when we use  c.accountid,because we use c.accountid in mapcon.put(c.accountid,listcon.size()).so how it is working there when we use   mapcon.get(ac.id),because the map dont know that ac.id,it only knows c.accountid.

please help me.

thanks,
B.Mahanandeesh.

 
Amit Chaudhary 8Amit Chaudhary 8
Both are same acc.id means Account Id and c.accountid means account id which is associated with contact. In both case we are using account id get record
This was selected as the best answer
belabela
Hi Amit,
that means as there is relationship only,we can able to call in this way,other wise we cant is it right?

Thanks
Mahanandeesh.
Amit Chaudhary 8Amit Chaudhary 8
Yes because On Contact we we have lookup of account and same field we are trying to fetch record from map .

So Account Id will same on Account and Contact object (For account lookup field)