+ Start a Discussion
@ M  Coder@ M Coder 

Simple Trigger on Maps

Hello Folks , 

Need help in implementing using maps ,  Not understanding how to do it . Can anyone please help me with code

If I have Account1 and Contact1 and Contact2 are related to Account1. Then, in account object detail page, i have a text box named Account.ContactName__c =  Contact1, Contact2  etc . i need to diaplay the related contact names on Account .  i did it in list but not able to do it in maps . 

Using  List : 
for(Account  a:acclist )
      {
          String contactName = '';
          for(contact  c :a.contacts )  
           {
           contactName = c.name+',' ;
           }
       a.ContactName__c = contactName;
       
      }
      update acclist; 
Best Answer chosen by @ M Coder
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

The below will help to update the account field 'contactMembers' with the contacts name related to that account using map
List<Account> accList=new List<Account>();
Map<Id,Account> accMap=new Map<Id,Account>([select id,name,contactMembers__c from Account]);
List<Contact> conList=new List<Contact>([select id,AccountId from contact where AccountId!=null]);
Map<Id,String> conMap=new Map<Id,String>();
for(contact c:conList)
{
if(conMap.containsKey(c.AccountId))
{
String conNames=conMap.get(c.AccountId);
conNames+=','+c.name;
conMap.put(c.AccountId,conNames);
}
else
{
    conMap.put(c.AccountId,c.name);
}
}
for(Account acc:accMap.values())
{
if(conMap.containsKey(acc.id))
{
acc.contactMembers__c=conMap.get(acc.id);
accList.add(acc);
}
}
system.debug(accList);
update accList;

All Answers

Rahul.MishraRahul.Mishra
Hi,

To do the same thing through map, we will create a Map of Parent Account id with child contact name. In case of first contact we will put simply, next time when contact inserted with same parent then we will put comma then contact name.
Once this map gets ready we will update those account whose Id we stroed in map with string which we have of contact name belongs to that account, so based on that Id we will update account. Here is the code:
 
Map<Id, String> mapOfAccIdToStr= new Map<Id, String>();
List<Account> lstAccountToUpdate  = new List<Account>();

 for(Account  a:acclist )
      {	
		
		   for(contact  c :a.contacts)  
           {
		   
		    if(!mapOfAccIdToStr.containsKey(a.Id)) {
			   mapOfAccIdToStr.put(a.Id, c.Name);
			} else {
			   String str = mapOfAccIdToStr.get(a.Id);
			   str = str+','+c.Name);
			
			}
		}	
	}	
	if(!mapOfAccIdToStr.isEmpty() {
	for Id accId : mapOfAccIdToStr.keySet() {
		Account acc = new Account(Id = accId, ContactName__c = mapOfAccIdToStr.get(accId);
		lstAccountToUpdate.add(acc);
	}
	
	}
	if(!lstAccountToUpdate.isEmpty()) 
	  update lstAccountToUpdate;

Mark answer as solved if it does helps you.

Thanks,
Rahul
 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

The below will help to update the account field 'contactMembers' with the contacts name related to that account using map
List<Account> accList=new List<Account>();
Map<Id,Account> accMap=new Map<Id,Account>([select id,name,contactMembers__c from Account]);
List<Contact> conList=new List<Contact>([select id,AccountId from contact where AccountId!=null]);
Map<Id,String> conMap=new Map<Id,String>();
for(contact c:conList)
{
if(conMap.containsKey(c.AccountId))
{
String conNames=conMap.get(c.AccountId);
conNames+=','+c.name;
conMap.put(c.AccountId,conNames);
}
else
{
    conMap.put(c.AccountId,c.name);
}
}
for(Account acc:accMap.values())
{
if(conMap.containsKey(acc.id))
{
acc.contactMembers__c=conMap.get(acc.id);
accList.add(acc);
}
}
system.debug(accList);
update accList;
This was selected as the best answer
Akshay_DhimanAkshay_Dhiman
Hi,

try this code
 
trigger ContactLastNameArray on Contact (after insert ,after update ,after delete) {
    Set<Id> accid = new Set<Id>();
 List<Account> accList = new List<Account>();
 List<Account> accToUpdate= new List<Account>();
    List<Contact> conList = new List<Contact>();
  
    if(Trigger.isInsert ){
        For(Contact c : Trigger.New){
            accid.add(c.accountId);
        }
    }
 if(Trigger.isUpdate || Trigger.isDelete ){
        For(Contact c : Trigger.Old){
            accid.add(c.accountId);
        }
    }
 if(accid.size() > 0) {
  conList = [select Name,accountId from contact where accountId =:accid];
  accList = [select ContactName__c from Account where id IN:accId];
  map<id,List<Contact>> conMap = new map<id,List<Contact>>();
  for(Contact c :conList){
   if(conMap.get(c.AccountId)==null){
    conMap.put(c.AccountId,new List<Contact>());
   }
   conMap.get(c.AccountId).add(c);
   
  }
  for(Account ac :accList){
   if(conMap.containsKey(ac.Id)){
     String contactName = '';
      for(contact  c : conMap.get(ac.Id))  
       {
       contactName = c.Name+',' ;
       }
      ac.ContactName__c = contactName;
      accToUpdate.add(ac);
   }
  }
  if(accToUpdate.size() > 0){
   update accToUpdate;
  }
 }
}



if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
@ M  Coder@ M Coder
HI both , Thank you for the quick response . 

@Rahul : we may land in cpu time out exception if we use for inside for loop .  line 14 was comming error 

@Bhargavi Tunuguntla : It was working perfectly fine