You need to sign in to do that
Don't have an account?
Mohit Kumar Singhal
how to insert contact in account using apex
I have account and contact standard object . I want to insert data in account and contact object together using apex. contact details should be inserted into account . Please help me out
Mark as best answer, If it resloves !!
check these below code
more info : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_insert_update.htm
Mark as resloved if it helps :) :)
Thanks,
Bhanu Prakash
visit ForceLearn.com
Do you need contact details to be updated in the created account after account and contact insertion? -- just a clarificatioon
Thanks
Bhargavi
"Try this code it will create an account and a contact related to account." If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
trigger AccountTrigger on Account (after insert,after update) {
//Account has a number field, we need to create contacts as per this field
Map<Id,Integer> mapofAccountToCon = new Map<Id,Integer>();
List<Contact> lstOfContactsToBeInserted = new List<Contact>();
for(Account acc : Trigger.New){
mapofAccountToCon.put(acc.Id, acc.NumberOfEmployees);
}
for(Id acc : mapofAccountToCon.keyset()){
for(Integer i=0;i<mapofAccountToCon.get(acc);i++){
Contact con = new Contact();
con.FirstName = 'xyz'+i;
con.LastName = 'xyz'+i;
con.AccountId = acc;
lstOfContactsToBeInserted.add(con);
}
}
insert lstOfContactsToBeInserted;
}