You need to sign in to do that
Don't have an account?
bela
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
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
All Answers
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
Thanks
Amit Chaudhary
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.
that means as there is relationship only,we can able to call in this way,other wise we cant is it right?
Thanks
Mahanandeesh.
So Account Id will same on Account and Contact object (For account lookup field)