You need to sign in to do that
Don't have an account?
Patrick Conner
Bulkify Code to Create Accounts on Contact Upserts
I'm trying to bulkify the following code, which works well for a single contact and parent account creation:
Any help would be greatly appreciated! Thank you!
trigger createacc on Contact (after insert) { list<Contact > con=[select id,lastname,email from contact where id in:Trigger.newMap.keySet()]; list<account >acc =new list<account>(); list<contact>cc1 =new list<contact>(); for(contact c : con) { account a =new account (); a.name=c.lastname; a.email__c=c.email; acc.add(a); } insert acc; for(account a1:acc){ system.debug('---------------------this is id --------------------'+a1.id); for(contact c : con){ c.accountid=a1.id; cc1.add(c); } } update cc1; }
Any help would be greatly appreciated! Thank you!
I would suggest you to create two triggers for this requirement.
First trigger will simply insert the account records when contacts are inserted :
Now second trigger will be written on Account which will update all the related contacts with account id :
Please use both triggers for this requirement.
Let me know if you need more information on this.
Thanks,
Abhishek bansal