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
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