+ Start a Discussion
Sunil Kumar 545Sunil Kumar 545 

contact id as key and list of book obj using triggers

Ajay K DubediAjay K Dubedi
Hi Sunil,

Here is the piece of code, may it'll help you.

trigger:-
-------------------
trigger ContactwithCustomObj2 on Contact (before insert,before update) 
{
    ContactWithCustomObj.test(trigger.new);
}


Helper Class :-
-------------------
public class ContactWithCustomObj 
{
  public static void test(List<contact> conlist)
  {
      List<book__c> customlist=[select name from book__c where contact__c In:conlist];
       
        Map<Id, List<book__c>> mapAccountWithContacts = new Map<Id, List<book__c>>();
      if(customlist!=null){
      for(contact con : conlist){
          for(book__c custom : customlist){
       

      if(!mapAccountWithContacts.containsKey(custom.Contact__c)) {
           mapAccountWithContacts.put(Con.id, new List<book__c>());
      }
      mapAccountWithContacts.get(con.Id).add(custom);
        }
      }
      }
      system.debug('mapAccountWithContacts====='+mapAccountWithContacts);
  }
   
}

Please mark it as the best answer if it will help you.

Thanks,
Ajay Dubedi