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
Ankita Gupta 65Ankita Gupta 65 

Create master detail object records one after another in apex

Hi, I'm trying to create 2 object records in apex. I'm having issues in bukifying the code. I have a Map<A,B> where A is master object and B is child object. Once I insert A, I need to update the field in B with record ID of A. Can anyone tell me how can this code be bulkified.
v varaprasadv varaprasad

trigger UpdateAccountid on Contact (after insert) {
   UpdteAccountIdOnContact.updateTitle(trigger.new);
}




public class UpdteAccountIdOnContact {
    public static void updateTitle(list<contact> lstContats){
        list<contact> lstOFCons = new  list<contact>();
        for(contact c : lstContats){
            if(c.accountid != null){
                contact cc = new contact();
                cc.id = c.id;
                cc.Title = c.accountid;
                lstOFCons.add(cc);
                
            }
            
        }
        
        if(lstOFCons != null){
            update lstOFCons;
        }
        
    }

}

Please check once above code and let me know.

Thanks
Varaprasad
Ankita Gupta 65Ankita Gupta 65
@v varaprasad
In this case you've inserted child records after insert.. lets say this is before insert