I have a customObject where users can fill in information for an account,contact, and campaign. I'm trying to write a trigger where once a user can creates this customObject, then an account, contact, and campaign will be created with the populated fields. I've gotten as far as creating the account but now I'm wondering how I can get the account ID in order to create a new contact and campaign that are linked to the newly created Account. My code is shown below.
trigger nameOfTrigger on customObject (after insert, after update) {
List<Account> AccsForInsert = New List<Account>();
List<Contact> ContactsForInsert = New List<Contact>();
List<Campaign> CampaignForInsert = new List<Campaign>();
if(Trigger.isInsert && trigger.new.size()==1){
for(customObject z: Trigger.new){
Account a = new Account
Contact con = new Contact();
Campaign cam = new Campaign();
a.Name = z.name;
a.Emply_for_Ongoing_Edit__c = decimal.valueOf(z.Emply_Size__c);
a.BillingStreet = z.Street__c;
a.BillingCity = z.city__c;
a.BillingState = z.state__c;
a.BillingCountry = z.Country__c;
a.BillingPostalCode = z.Zip_Postal_Code__c;
a.website = z.website__c;
AccsForInsert.add(a);
if(AccsForInsert.size()>0){
insert AccsForInsert;
}
//This is where I'm stuck
//con.AccountId = newlyCreatedAccountId
con.FirstName = z.first_name__c;
con.LastName = z.last_name__c;
con.title = z.title__c;
con.email = z.email__c;
con.phone = z.phone__c;
ContactsForInsert.add(con);
if(ContactsForUpdate.size()>0){
insert ContactsForInsert;
}
/*
cam.Co_Sponsoring_Account__c = x.sponsor__c;
cam.name = x.name;
cam.country = x.country__c;
CampaignForInsert.add(cam);
if(CampaignForInsert.size()>0){
insert CampaignForInsert;
}
*/
}
}
}
}